check regular expression in javascript code example
Example 1: how to validate a string using regular expression in javascript
function validate(){
var phoneNumber = document.getElementById('phone-number').value;
var postalCode = document.getElementById('postal-code').value;
var phoneRGEX = /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/;
var postalRGEX = /^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$/i;
var phoneResult = phoneRGEX.test(phoneNumber);
var postalResult = postalRGEX.test(postalCode);
alert("phone:"+phoneResult + ", postal code: "+postalResult);
}
Example 2: javascript regex
let reg = /abc/
reg = new RegExp('abc')
let str = 'Abc abc abc'
str.match(/abc/)
str.match(/abc/g)
str.match(/abc/i)
str.match(/abc/ig)
str.match('abc', 'ig')
Example 3: regular expression javascript
if (/^https\:\/\/example\.com\/$/.exec(document.location)){
console.log("Look mam, I can regex!");
}