1

I am trying to replace all occurrences of a regexp pattern with a new word. Currently I can only replace the coinsurance if it is an individual word that separated by space " ", but I would like to replace all of them even if they are in the middle of a word. Here is an example:

for string: abc:target12 target12 cdtarget23 target

I would like to replace all the occurrences of target[0-9]{2} with "ok", so after the replacement, the new string would be like: abc:ok ok cdok target

Thanks!

0

2 Answers 2

1

I think you are missing the g (Global) at the end of your regex (demo):

alert("abc:target12 target12 cdtarget23 target".replace(/target[0-9]{2}/g, 'ok'))
Sign up to request clarification or add additional context in comments.

Comments

1

Use replace like this:

var repl = str.replace(/target\d{2}/g, 'ok');

Live Demo: http://ideone.com/hew6oa

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.