javascript escape all special characters code example
Example 1: remove special characters from string javascript
var str = "Hello^# World/";
str.replace(/[^a-zA-Z ]/g, "");
Example 2: js remove special characters
var desired = stringToReplace.replace(/[^\w\s]/gi, '')
Example 3: javascript escape regex
function escapeRegex(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
const escapeRegex = require('escape-string-regexp');
const regex = new RegExp(escapeRegex('How much $ is that?'));
Example 4: javascript escape regex
function escapeRegExp(input) {
const source = typeof input === 'string' || input instanceof String ? input : '';
return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}