Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

javascript - Javascript正则表达式选择多个(Javascript regular expression selecting multiple)

my problem is selected expressions cant use more than one but i need it(我的问题是所选表达式不能使用多个表达式,但我需要)

here i prepare a example of what i want(在这里,我准备一个我想要的例子)

 var regexp = /(^| )([az]{1})( |$)/gim; var string = "h ello exa mple"; var choosen = string.match(regexp); for(var i = 0; i < choosen.length; i++){ console.log(choosen[i]); } 

you can see this only chooses "h ", " e ", " a "(您会看到这只会选择"h ", " e ", " a ")

but i want to choose "h", "e", "x", "a" without any " "(但我想选择"h", "e", "x", "a"而没有任何" ")

i know i can do it without regexp but this is really important for me(我知道不用regexp就可以做到,但这对我来说真的很重要)

  ask by Mert ?elik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your case, you may simply try matching all groups consisting of non separator characters:(就您而言,您可以简单地尝试匹配由非分隔符组成的所有组:)

 var input = "h ello ex ample"; var matches = []; var regexp = /\b[^\s]\b/g; match = regexp.exec(input); while (match != null) { matches.push(match[0]); match = regexp.exec(input); } console.log(matches); 

The regex pattern \b[^\s]\b seeks to match any single non whitespace character which is also bounded on both sides by word boundaries.(正则表达式模式\b[^\s]\b试图匹配任何单个非空格字符,该字符在两侧均由单词边界界定。)

In this case, it translates to matching the single letters (though it could also match other things, depending on a different input).(在这种情况下,它转换为匹配单个字母(尽管也可以匹配其他内容,具体取决于不同的输入)。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...