我匹配以某些前缀开头的单词。
这个正则表达式对我来说效果很好但我在一场比赛中应用多个术语时遇到了麻烦。
/re\S+/g;
我认为它适用于
/(re|http)\S+/g;
然而,后者仅返回第二个词的匹配。
以下是完整代码:
function replacePrefix(input){
var re = /(#)\S+/g;
var specials = [];
var match;
while(match = re.exec(input)){
$('#text').html(input.replace(specials[0], "<span class='special'>"+specials[0]+"</span>"));
}
}
答案 0 :(得分:1)
function replacePrefix(input){
var re = /\b(?:re|http)\S+/g;
var specials = [];
var match;
while(match = re.exec(input)){
console.log(match);
}
}
replacePrefix("This is a sentence with really great urls in it like http://coolurl.com";);
["really"]
["http://coolurl.com"]