var desired = stringToReplace.replace(/[^\w\s]/gi, '')
As was mentioned in the comments it's easier to do this as a whitelist - replace the characters which aren't in your safelist.
The caret (^) character is the negation of the set [...], gi say global and case-insensitive (the latter is a bit redundant but I wanted to mention it) and the safelist in this example is digits, word characters, underscores (\w) and whitespace (\s).
var desired = stringToReplace.replace(/[^\w\s]/gi, '')
As was mentioned in the comments it's easier to do this as a whitelist - replace the characters which aren't in your safelist.
The caret (^) character is the negation of the set [...], gi say global and case-insensitive (the latter is a bit redundant but I wanted to mention it) and the safelist in this example is digits, word characters, underscores (\w) and whitespace (\s).
Note that if you still want to exclude a set, including things like slashes and special characters you can do the following:
var outString = sourceString.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
take special note that in order to also include the "minus" character, you need to escape it with a backslash like the latter group. if you don't it will also select 0-9 which is probably undesired.
Javascript string replace with regex to strip off illegal characters - Stack Overflow
javascript - Regex to remove specific special characters
How can I remove a character from a string using JavaScript? - Stack Overflow
Regex remove all special characters except numbers?
Hi all,
I'm passing a string that looks like this:
[("MT-1","MT-2","MT-3","MT-4")] I want to write a Javascript function which converts the string to this:
MT-1,MT-2,MT-3,MT-4
Essentially I want to remove ([]"") characters. Is there a way to do this using Regex commands?
I looked at this link but couldn't find anything about removing special characters.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet
My code so far:
var x = "[("MT-1","MT-2","MT-3","MT-4")]";
x = xxx.replace(/[^0-9]+/g, "");What you need are character classes. In that, you've only to worry about the ], \ and - characters (and ^ if you're placing it straight after the beginning of the character class "[" ).
Syntax: [characters] where characters is a list with characters.
Example:
var cleanString = dirtyString.replace(/[|&;$%@"<>()+,]/g, "");
I tend to look at it from the inverse perspective which may be what you intended:
What characters do I want to allow?
This is because there could be lots of characters that make in into a string somehow that blow stuff up that you wouldn't expect.
For example this one only allows for letters and numbers removing groups of invalid characters replacing them with a hypen:
"This¢£«±Ÿ÷could&*()\/<>be!@#$%^bad".replace(/([^a-z0-9]+)/gi, '-');
//Result: "This-could-be-bad"
Use the string.replace() method and the unicode escape codes for your special characters. You have to use the \ character to escape in JavaScript regex
var str = 'Here is a \' string \" with \" \' some @ special ® characters ™ & &'
str.replace(/['"\u0040\u0026\u2122\u00ae]/g, '')
/pattern/ denotes a regex pattern in JavaScript
[charset] says which set of characters to match
/g specifies global matches so it will replace all occurrences
console.info(`a'b"c&d®e™f`.replace(/['"&®™]/g, ''));
var mystring = "crt/r2002_2";
mystring = mystring.replace('/r','/');
will replace /r with / using String.prototype.replace.
Alternatively you could use regex with a global flag (as suggested by Erik Reppen & Sagar Gala, below) to replace all occurrences with
mystring = mystring.replace(/\/r/g, '/');
EDIT: Since everyone's having so much fun here and user1293504 doesn't seem to be coming back any time soon to answer clarifying questions, here's a method to remove the Nth character from a string:
String.prototype.removeCharAt = function (i) {
var tmp = this.split(''); // convert to an array
tmp.splice(i - 1 , 1); // remove 1 element from the array (adjusting for non-zero-indexed counts)
return tmp.join(''); // reconstruct the string
}
console.log("crt/r2002_2".removeCharAt(4));
Since user1293504 used the normal count instead of a zero-indexed count, we've got to remove 1 from the index, if you wish to use this to replicate how charAt works do not subtract 1 from the index on the 3rd line and use tmp.splice(i, 1) instead.
A simple functional javascript way would be
mystring = mystring.split('/r').join('/')
simple, fast, it replace globally and no need for functions or prototypes
Using replace() with regular expressions is the most flexible/powerful. It's also the only way to globally replace every instance of a search pattern in JavaScript. The non-regex variant of replace() will only replace the first instance.
For example:
var str = "foo gar gaz";
// returns: "foo bar gaz"
str.replace('g', 'b');
// returns: "foo bar baz"
str = str.replace(/g/gi, 'b');
In the latter example, the trailing /gi indicates case-insensitivity and global replacement (meaning that not just the first instance should be replaced), which is what you typically want when you're replacing in strings.
To remove characters, use an empty string as the replacement:
var str = "foo bar baz";
// returns: "foo r z"
str.replace(/ba/gi, '');
ONELINER which remove characters LIST (more than one at once) - for example remove +,-, ,(,) from telephone number:
var str = "+(48) 123-456-789".replace(/[-+()\s]/g, ''); // result: "48123456789"
We use regular expression [-+()\s] where we put unwanted characters between [ and ]
(the "\s" is 'space' character escape - for more info google 'character escapes in in regexp')
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;
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.