:
js> s = "ao123eu";
"ao123eu"
js> if (s.match(/\d/) === null) { print("has no number"); } else { print("has number") }
has number
js> s = "aoeu";
"aoeu"
js> if (s.match(/\d/) === null) { print("has no number"); } else { print("has number") }
has no number
내가 \d
에 대해 문자열에 정확히 한 번만 나타나는 숫자와 일치한다하고있어 모든.
그리고 당신은 그것을 기능을 할 수 있습니다 : 문자열에 숫자가 있다면 true를 돌려
function has_number(s) {
return s.match(/\d/) !== null;
}
합니다. 이제 당신은 대체 수행하려는 경우
, 당신이 할 수 있습니다
function remove_numbers(s) {
return s.replace(/\d+/g, "\\_o<")
}
주는 :
var re = /^\D*$/mg,
line;
while ((line = re.exec(str)) !== null) {
console.log(line);
}
:
js> print(remove_numbers("ao234u 42 ao1234eu"))
ao\_o<u \_o< ao\_o<eu
출처
2014-05-08 21:07:33
zmo
많은 문자열을 대체 할 문자열을 사용하려고합니다. 적절하지 않다고 생각합니다. – auragar
사소한 점 : 당신이 * + any * 숫자가 일치를 무효로 할 필요가있는'+'필요가 없다. – usr2564301
글쎄, 난 네 질문에 대답하고있어, 내가 네게 준 작품들! 제 대답이 당신이 필요로하는 것과 일치하지 않으면 또 다른 질문을하십시오! – zmo