How to search and replace all words "Hello" and "hello" in string "Hello, lorem ipsum hello and phelloderm" with exception substring in "phelloderm"
Asked
Active
Viewed 39 times
-3
-
1Could be done with word-boundary characters. Look this up. – Sergio Tulentsev Feb 01 '18 at 12:35
-
What code you have written so far? – Supriya Feb 01 '18 at 12:35
-
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) – Alex Shesterov Feb 01 '18 at 12:36
-
@AlexShesterov: not _quite_ a duplicate of this one. Only partially. :) – Sergio Tulentsev Feb 01 '18 at 12:40
1 Answers
1
Use word boundary \b
"Hello, lorem ipsum hello and phelloderm".replace( /\bhello\b/gi, "$1" )
outputs
"$1, lorem ipsum $1 and phelloderm"

gurvinder372
- 66,980
- 10
- 72
- 94