Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Javascript and RegEx for removing special characters

I have a string in javascript with some special characters inside.

var string = xxxx † yyyy § zzzz 

And I would like to remove those special characters and get only :

string = xxxx yyyy zzzz

I have tryed with this regex:

&#?[a-z0-9]+;

Like that:

string = string.replace(/&#?[a-z0-9]+;/g, "");

But the special characters are not matched with this regex.

Do you please have an idea how can I do it ?

The regex works well, see the example: http://regexr.com?31rrj

Upvotes: 1

Views: 6048

Answers (4)

maerics
maerics

Reputation: 156364

You could remove HTML entities with a regex like so:

function removeEntities(str) {
  return (''+str).replace(/&\w+;\s*/g, '');
}

removeEntities("xxxx † yyyy § zzzz"); // => "xxxx yyyy zzzz"

Upvotes: 1

Disco Banana
Disco Banana

Reputation: 102

What about using JavaScript's string.replace() method? It's probably faster, and definitely more readable than RegEx, though it might take a few lines of code.

Here's a whole writeup on replace http://www.bennadel.com/blog/142-Ask-Ben-Javascript-String-Replace-Method.htm - and Ben uses RegEx with string.replace as well. This should have everything you need.

Upvotes: 1

Overcode
Overcode

Reputation: 4324

It's working fine for me.
Working example: http://jsfiddle.net/JwrZ6/

It's probably your syntax, strings have to be defined with " around them.

var string = "xxxx † yyyy § zzzz";

NOT

var string = xxxx † yyyy § zzzz;

Upvotes: 4

Paul T. Rawkeen
Paul T. Rawkeen

Reputation: 4114

You should use RegExp as follows:

string.replace( new RegExp( regexp_expression, 'g' ), '' );

Syntax for RegExp class is next:

var regexp = new RegExp(pattern [, flags]);

You can read documentation on this class.

Upvotes: 1

Related Questions