1

I'm trying to dynamically replace a sub string in a parent string. I've tried string.replace('old','new') to no avail.

Here's the snippet giving me trouble.

newbuffer.replace( matches[i], p.shortcodes[x].vala );
console.log( "Replace " + matches[i] + " with " + p.shortcodes[x].vala );

Result is

"Replace [hello] with <span class="strong"><p>Awaiting ajax request...</p></span">"

Am I missing something here? Does .replace take "plain text" or will I need to do some regex?

1
  • Replace takes both plain text and regex. What is in newbuffer variable? Commented Aug 14, 2014 at 8:33

1 Answer 1

3

replace returns a new string with the replacement, it doesn't change the string you call it on.

newbuffer = newbuffer.replace( matches[i], p.shortcodes[x].vala );

Does .replace take "plain text" or will I need to do some regex?

You can call replace either with a string as the "search" argument, or with a regex. If you give it a string, it will replace the first occurrence of that string. If you give it a regex, it will replace the first match, or all matches, depending on whether you include the g ("global") flag on the regex.

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

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.