0

How to replace '$txt$' in javascript.I want to replace all occurrences in the string

It's what i tried

if (html.indexOf('$txt$') > -1) {
    html = html.replace(/$txt$/ig, '<input type=text></input>');
}

but it won't replace the string.What's my mistake.Please help me

0

3 Answers 3

8

all you need to do is escape the $ sign since it has a meaning in a regular expression. Change it to

html = html.replace(/\$txt\$/ig, '<input type="text" />');

and it should be fine :)

edit: $ means end of line in a regular expression :)

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

Comments

0

html = html.replace(/[$]txt[$]/ig, ''); use this

Comments

0

You're replacing using regular expressions, and $ is a regular expression reserved special character for end of line/string.

You need to escape the $ by preceding it with a backward slash \ to make it hit its literal character:

if (html.indexOf('$txt$') > -1) {
    html = html.replace(/\$txt\$/ig, '<input type=text></input>');
}

1 Comment

that's a backward slash, what is used to escape a speial character.

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.