Ram Kumar
Ram Kumar

Reputation: 590

jquery parse text within braces

I want to remove

[[Have to keep this text]] this {{ remove this junk }} and this.

I implemented the above and it is working at : http://jsfiddle.net/DMGdG/ I used this : https://raw.github.com/cowboy/jquery-replacetext/master/jquery.ba-replacetext.min.js

but in my server(127.0.0.1) the same code is not doing the trick, there i tried both the ways

str.replace(regex, charecter)

and the one mentioned at jsfiddle.

My queries:


jstr.replace("/\s*\{{.*?\}}\s*/g", " "); // for removing curly braces and text within.

jstr.replace("/[\[(.)\]]/g", ""); // to replace the square braces.

index.html has only a <p> Sample Text(as shown above)</p>

Upvotes: 4

Views: 1778

Answers (2)

Ken Redler
Ken Redler

Reputation: 23943

Instead of this:

$("p").replaceText(/\{\{.+\}\}/U/gi, '****' );

Try this:

$("p").replaceText(/\{\{.+?\}\}/gi, '****' );

The addition of the question mark makes the matched pattern non-greedy -- that is, it will match the very next }} it encounters, rather the the very last one.


Edit: You've stated this doesn't work for you. Perhaps omitting the replaceText plug-in and using plain old replace() instead will help:

$("p").each( function(){
    $(this).text( $(this).text().replace(/\{\{.+?\}\}/gi, '****' ) );
});

Again, it seems to work in your fiddle when edited as above. Perhaps the problem is with the plug-in.

Upvotes: 1

Onur Yıldırım
Onur Yıldırım

Reputation: 33702

This works:

$(function()
{
    $("p").replaceText(/\{\{.*?\}\}/gi, '' ); //removes {{...}}
    $("p").replaceText(/\[\[(.*?)\]\]/gi, '$1' ); //removes [[ and ]] around text
}); 

Upvotes: 0

Related Questions