Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私の質問は次のとおりです。たとえば、出力 7 を取得するために (55)-555-34 がある場合、文字列から数字のみをカウントするにはどうすればよいですか。たとえば、ダッシュとブラケットを除外することを意味します。乾杯!
正規表現で.match()を使用できます。/\d/g
/\d/g
"(55)-555-34".match(/\d/g).length //result=>7
で数値以外をすべて削除しreplace、結果の文字列の長さを取得します。
replace
str.replace(/\D/g,"").length
matchこれには、結果を確認する必要がないという利点がありnullます (一致が見つからない場合)。
match
null