最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript - Replace only if there is a perfect match - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 4

You 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);

发布评论

评论列表(0)

  1. 暂无评论