special characters regex javascript code example

Example 1: regex special characters javascript

var regex = /^[a-zA-Z0-9!@#\$%\^\&*\)\(+=._-]+$/g

Example 2: how to check for special characters in javascript

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;

if(format.test(string)){
  return true;
} else {
  return false;
}

Example 3: javascript escape regex

function escapeRegExp(input) {
	const source = typeof input === 'string' || input instanceof String ? input : '';
	return source.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}

Example 4: regex for strings with specific letters javascript

let re = /ab+c/;

Example 5: regular expression javascript

// Tests website Regular Expression against document.location (current page url)
if (/^https\:\/\/example\.com\/$/.exec(document.location)){
	console.log("Look mam, I can regex!");
}

Tags:

Sql Example