0

Here is one example of what I need.

If I have some text like this one:

var string = "This is some test string for some my test.";

Then I have Regex like this one:

var patern = new RegExp("\\bsome\\b", 'gi');

and then line for replace:

var new_string = string.replace(patern,"replace_string");

This line above will replace all words "some" in string with "replace_string".

 new_string = "This is replace_string test string for replace_string my test."

What I need is to limit it to replace it just once. Is that possible, and how?

2
  • 5
    Don't supply the "g" flag if you don't want to do a global replace. Commented Mar 10, 2015 at 16:43
  • Thanks, In meanwhile I figured it out. I just was to delete question and you commented :) Commented Mar 10, 2015 at 16:45

2 Answers 2

3

Don't pass 'g' (global) as the second argument in the regex, maybe try something like str.replace(pattern); or str.replace(pattern, 'i'); to keep ignoring case.

Sign up to request clarification or add additional context in comments.

Comments

0
'This is some test string for some my test.'.replace(/\bsome\b/, 'replace_string'); 
// => "This is replace_string test string for some my test."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.