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:
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.?31rrj
Share Improve this question asked Aug 16, 2012 at 14:27 Milos CuculovicMilos Cuculovic 20.2k54 gold badges164 silver badges276 bronze badges 1- 1 This works fine... jsfiddle/MKjb3 – canon Commented Aug 16, 2012 at 14:36
4 Answers
Reset to default 4It's working fine for me.
Working example: http://jsfiddle/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;
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.
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./blog/142-Ask-Ben-Javascript-String-Replace-Method.htm - and Ben uses RegEx with string.replace as well. This should have everything you need.
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"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744237730a4564551.html
评论列表(0条)