You should use the string replace function, with a single regex. Assuming by special characters, you mean anything that's not letter, here is a solution:
const str = "abc's test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from Petar Ivanov on Stack OverflowYou should use the string replace function, with a single regex. Assuming by special characters, you mean anything that's not letter, here is a solution:
const str = "abc's test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can do it specifying the characters you want to remove:
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
Alternatively, to change all characters except numbers and letters, try:
string = string.replace(/[^a-zA-Z0-9]/g, '');
javascript - Remove all special characters with RegExp - Stack Overflow
javascript - Regex remove all special characters except numbers? - Stack Overflow
javascript - how to remove Characters from a string in react.js - Stack Overflow
Javascript regex - remove all special characters except semi colon - Stack Overflow
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.
you can use regex and string.replace
string.replace(/[|&;$%@"<>()+,]/g, "");
just put whatever string you want to ignore inside the rectangular brackets, i.e - string.replace(/[YOUR_IGNORED_CHARACTERS]/g, "")
you can read more about regex here
Use this regex:
const regex = /\('([^']+)',\)/g
const output = input.replace(regex, '$1');
// explain: / \( ' ( [^']+ ) ',\) / g
// 1 2 3 4 5 6 7 8 9
- 1: start regex
- 2: match character
((character/to escape) - 3: match character
' - 4: start group in regex
- 5: match character not
' - 6: close group in regex
- 7: match
',) - 8: end regex
- 9: make this regex match global (don't need if you want replace one time)
$1in code is first group match regex (start with 4 and end with 6)
const input = `('FCA',)
('JP NIC',)
('JP CHI',)
('2022-03-04T07:18:36.468Z',)`;
const output = input.replace(/\('([^']+)',\)/g, '$1');
console.log(input)
console.log('// =>')
console.log(output)
You can use a regex that removes anything that isn't an alpha character or a semicolon like this /[^A-Za-z;]/g.
const str = "ABC/D A.b.c.;Qwerty";
const result = str.replace(/[^A-Za-z;]/g, "");
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
var str = "ABC/D A.b.c.;Qwerty";
var result = str.replace(/[^A-Za-z;]/g, ""); // 21ABCDAbc;Qwerty
Live DEMO
Simply replace it with nothing:
var string = 'F0123456'; // just an example
string.replace(/^F0+/i, ''); '123456'
Honestly I think this probably the most concise and least confusing, but maybe that is just me:
str = "F0123456";
str.replace("f0", "");
Dont even go the regular expression route and simply do a straight replace.