0

This is Replace text script.

I call it like this:

$(document).ready(function() {
$('#test-id').replaceText(/\[this\]/g,'<b>').replace(/\[\/this\]/g,'</b>');
});

It should make bold [this] bold text [/this] only, HTML below:

<span id="test-id"> [this] This is some text that contains [/this] red color with bold text</span>

It should bold text only inside this [this][/this] , but problem is it also make bold others the text and not hide last [/this] one.

I also tried to call this one:

$('#test-id').replaceText(/\[this\]/g,'<b>');

So please advice how to replace only [this] This is some text that contains [/this], not other text and hide last [/this] one.

Please see Fiddle: http://jsfiddle.net/vw5b1szm/1/

Thanks.

1 Answer 1

1

You can do

$('#test-id').replaceText(/\[(\/)?this\]/g, '<$1b>');

Demo: Fiddle

Since you are doing dom based replacements you can't do it in 2 steps ie [this] and [/this] can't be replaced in 2 different calls instead you need to replace it in a single call to replaceText. Also you second replace is using replace not replaceText

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

3 Comments

Hi, Arun one more thing is, when <span id="test-id"> [this] This is <i>some</i> text [/this] </span> Then it's not working, please help.
that is a problem with the plugin... it does not support text with dom elements in it to be replaced
What is the line in this plugin we should Change, there any advice please.

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.