2

I need to replace all matching words or special characters in a string, but cant figure out a way to do so.

For example i have a string: "This - is a great victory"

I need to replace all - with + signs. Or great with unpleasant - user selects a word to be replaced and gives replacement for it.

"\\b"+originalTex+"\\b" 

was working fie until i realised that \b does work only with word characters.

So the question is: what is replacement for \b would let me replace any matching word that is enclosed by whitespaces?

EDIT: I can not remove word boundaries as it would result inexact match. For example: you are creator of your world, while change you, your also would be changed. as it contains "you"

user3482211
  • 446
  • 4
  • 17
  • If the linked question does not help you, please edit your question to describe in what way your problem differs. Mind that right now your problem is the word boundaries, and removing them you get the desired behavior. – Wiktor Stribiżew Apr 03 '17 at 17:35
  • I've edited the answer. Cannot remove word boundaries as exact match is required – user3482211 Apr 03 '17 at 18:44
  • Ok, but you did not explain what the words you are searching for are. `-` is not a word from a linguistic point of view. What is your definition of a "word" here? – Wiktor Stribiżew Apr 03 '17 at 18:48
  • Any character(or sequence) that is in start or end of string OR is being surrounded by whitespaces. I've accomplished half of the task with lookahead, but it seems that lookbehind is missing in javascript. – user3482211 Apr 03 '17 at 18:50

2 Answers2

2

You need to use the following code:

var s  = "you are creator of your world";
var search = "you";
var torepl = "we";
var rx = new RegExp("(^|\\s)" + search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "(?!\\S)", "gi");
var res = s.replace(rx, "$1" + torepl);
console.log(res);

The (^|\\s) will match and capture into Group 1 start of string or a whitespace. The search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') will escape special chars (if any) inside the search word. The (?!\\S) lookahead will require a whitespace or end of string right after the search word.

The $1 backreference inserts the contents of Group 1 back into the string during replacement (no need to use any lookbehinds here).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

How about two replaces

var txt = "This - is a great, great - and great victory"
var originalTex1 = "great",originalTex2 = "-",
re1 = new RegExp("\\b"+originalTex1+"\\b","g"), 
re2 = new RegExp("\\s"+originalTex2+"\\s","g") 

console.log(txt.replace(re1,"super").replace(re2," + "))
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • This ALMOST works. Problem is that \\s includes whitespace in front so if symbol is being replaced then it will be joined with preceding string. – user3482211 Apr 03 '17 at 18:45
  • 1
    This is actually a good idea for my problem. I had something like `console.log(5)` and couldn't figure out how to match any other word like `a` in `and a person in a room`. From my understanding the next replace acts as a fail safe in case the previous one does nothing. Thank you, upvoted :) – lbragile Nov 17 '20 at 06:29