admin管理员组文章数量:1336139
I am currently studying javascript and I stumbled upon this problem. Is there a way I can specify to replace the words only if there is a perfect match?
PS: I thought that 'g' will fix the problem but it only did it half-way.
var text = "Isabella is moving to New York in March";
var words = ["is","in"];
var bine = new RegExp(words.join("|"),"g");
text = text.replace(bine,"REPLACE");
console.log(text);
I am currently studying javascript and I stumbled upon this problem. Is there a way I can specify to replace the words only if there is a perfect match?
PS: I thought that 'g' will fix the problem but it only did it half-way.
var text = "Isabella is moving to New York in March";
var words = ["is","in"];
var bine = new RegExp(words.join("|"),"g");
text = text.replace(bine,"REPLACE");
console.log(text);
Share
Improve this question
edited Sep 4, 2018 at 12:26
beaver
5271 gold badge9 silver badges21 bronze badges
asked Apr 5, 2017 at 18:29
George SmithGeorge Smith
5058 silver badges33 bronze badges
2
- perfect match meaning...??? – ibrahim mahrir Commented Apr 5, 2017 at 18:30
- @ibrahimmahrir perfect match is different from a partial match, as in; it would not match banana in "a pile of bananas" but would in "a banana pile" – Iesus Sonesson Commented Apr 5, 2017 at 18:35
4 Answers
Reset to default 4You can wrap the joined words in a group (preferably non-capturing) and then surround the groub with word boundaries \b
so the regexp will look like this:
/\b(?:word1|word2|...|wordn)\b/g
Change:
var bine = new RegExp("\\b(?:" + words.join("|") + ")\\b", "g");
// ^^^^^^^^^^^ ^^^^^^^^^
Example:
var text = "Isabella is moving to New York in March";
var words = ["is","in"];
var bine = new RegExp("\\b(?:" + words.join("|") + ")\\b","g");
text = text.replace(bine," REPLACE ");
console.log(text);
Note: the g
modifier mean global (mean replace not only the first but all matches)
Only put spaces in words :)
var text = "Isabella is moving to New York in March";
var words = [" is "," in "];
var bine = new RegExp(words.join("|"),"g");
text = text.replace(bine," REPLACE ");
console.log(text);
By perfect match, I assume you mean match only the words themselves and not inside of words that may contain the characters for a word you are searching for. You are looking for the word boundary functionality of RegExp. Try this modified code. Note the \b
added to the end and start. Basically, it acts as an anchor that says match this RegEx only in the instance of a whole word matching the pattern. I also put the words in a capturing group so the word boundary applies to all of them.
var text = "Isabella is moving to New York in March";
var words = ["is","in"];
var bine = new RegExp("\\b(" + words.join("|") + ")\\b","g");
text = text.replace(bine,"REPLACE");
console.log(text);
different approach: parse string into array and work with this array
var text = "Isabella is moving to New York in March";
var words = ['in', 'is'];
var result = text.split(' ').filter(el => {return !words.includes(el)}).join(' ');
console.log(result);
本文标签: regexJavascriptReplace only if there is a perfect matchStack Overflow
版权声明:本文标题:regex - Javascript - Replace only if there is a perfect match - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742384293a2464715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论