예를 들어 "Pay £ 100.00"이라는 텍스트가 포함 된 HTML 요소 ( "summary-payment")가 있습니다.
나는 다음과 같이 이것에서 숫자 100을 얻었습니다.
function getPaymentAmount(paymentAmount) {
var stringTotal = $("#paymentBtn").text();
var extractIntFromTotal = stringTotal.match(/\d+/)[0];
return extractIntFromTotal;
}
그러나 소수점 100.00을 되찾고 싶습니다. 색인에 추가하려고했지만 작동하지 않는 것 같습니다. 누구든지 도와 주시겠습니까? 나는 자바 스크립트를 처음 접했습니다.
감사합니다 :)
이를 달성하는 한 가지 방법은 소수를 포함하도록 정규식을 수정하는 것입니다.
function getPaymentAmount(paymentAmount) {
return paymentAmount.match(/\d+[.,]?\d{2}?/)[0];
}
console.log(getPaymentAmount('Pay £100.00'));
console.log(getPaymentAmount('Pay £215'));
console.log(getPaymentAmount('Lorem ipsum $500.33 dolor sit'));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다