1

Below is my string :

api/Node-1/{Node-1}/Node-1-1/{Node-1-1}

Search Term : Node-1

Replace with : Learning

Expected output :

api/Learning/{Learning}/Node-1-1/{Node-1-1}

Now here the problem is Node-1 is matching with other Node-1-1 also but I want exact word matching and replacement.

I have tried lots of options but none of them is working for me.

 function replaceAll(str, find, replace) {
        return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
    }

    function escapeRegExp(str) {
        return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    }
    
    console.log(replaceAll('api/Node-1/{Node-1}/Node-1-1/{Node-1-1}','Node-1','Learning'));
    
    
    var  replaceStr = 'Node-1';
    console.log( 'api/Node-1/{Node-1}/Node-1-1/{Node-1-1}'.replace(new RegExp("\\b"+replaceStr+"\\b","gi"),"Learning"));
    
    console.log( 'api/Node-1/{Node-1}/Node-1-1/{Node-1-1}'.replace(/\bNode-1\b/g,'Learning'));

Update : This question is not duplicate as my first answer is taken from this reference only which is not working with my input case.

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • Possible duplicate of [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – Liam Jun 20 '17 at 12:42
  • @Liam Please see that i have tried something and not just blindly posted here.I might have miss the link of what you have showed in your comment that doesnt mean i have not shown my efforts and i have taken answer from the reference only of what you have shared which is not working in my scenario.Thank you :) – I Love Stackoverflow Jun 20 '17 at 12:49

4 Answers4

3

Try this :

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

function escapeRegExp(str) {
  return str + '(?![-])';
}

console.log(replaceAll('api/Node-1/{Node-1}/Node-1-1/{Node-1-1}', 'Node-1', 'Learning'));

It searches every str occurence not followed by '-'. You can add other characters not to match if you want inside the character set.

Serge K.
  • 5,303
  • 1
  • 20
  • 27
  • Sorry but where to use this i mean from my 3 methods in which method i should use this? – I Love Stackoverflow Jun 20 '17 at 12:38
  • Ok upvoted for your kind efforts towards helping me but i didnt understand your answer and last last("You can add other characters not to match if you want inside the character set") – I Love Stackoverflow Jun 20 '17 at 12:58
  • I changed your regex generation to match the string given to `escapeRegExp` not followed by the **-** character. I also noticed you that you can add other characters to exclude inside the regex character set (ie. `[-]`). – Serge K. Jun 20 '17 at 13:05
  • Thank you so much for your kind help and please keep helping like this :) – I Love Stackoverflow Jun 20 '17 at 13:35
1

try this regex ^(?!.*Node-1\(?!-1))\w+.*

You can see it in action here

EDIT

So your code would be :

var str = 'api/Node-1/{Node-1}/Node-1-1/{Node-1-1}';
console.log(str.replace(new RegExp("Node-1\(?!-1)","gi"),"Learning"));

A working JsFiddle

Hamza Abdaoui
  • 2,029
  • 4
  • 23
  • 36
1

Hope this one fixes your problem:

var a = "api/Node-1/{Node-1}/Node-1-1/{Node-1-1}";

var arr = a.split("/");

for (var i=0; i<arr.length; i++) {

    var word = arr[i].replace(/[^a-zA-Z1-9- ]/g, "");
    if (word=="Node-1"){
        arr[i] = arr[i].replace("Node-1", "Learning");
    }
}

newString = arr.join("/");
console.log(newString);

check fiddle: https://jsfiddle.net/z8v0xt98/

treecon
  • 2,415
  • 2
  • 14
  • 28
1

Try this with the all-but-not expression

console.log('api/Node-1/{Node-1}/Node-1-1/{Node-1-1}'.replace(new RegExp("Node-1[^-]","gi"),"Learning"));
slashron
  • 277
  • 1
  • 4
  • 13