我无法弄清楚如何编写一个正则表达式模式来捕获在第一个数字之后开始的所有单词。为清晰起见,下面有两个例子:
var string1 = "Area 51"; // This is the string I have
var match1 = "51"; // This is the string I want
或者这个:
var string2 = "A simple sentence with 6 words or more" // This is the string I have
var matchedString = "6 words or more" // This is the string I want
有什么想法吗?
答案 0 :(得分:5)
您可以使用正则表达式 \b\d+\b.*$
和方法 match()
进行模式匹配
var string1 = "Area 51"; // This is the string I have
var match1 = string1.match(/\b\d+\b.*$/)[0];
var string2 = "A simple sentence with 6 words or more" // This is the string I have
var matchedString = string2.match(/\b\d+\b.*$/)[0]; // This is the string I want
document.write(match1+'<br>'+matchedString);
&#13;
\b\d+\b.*$
答案 1 :(得分:0)
使用\b
字边界后跟\d
来查找以数字开头的字词。
var str = "Simple test1 String 2where there rand3om number4s through5out.";
alert(str.match(/\b\d.*/)[0]);
&#13;