Javascript4 1
Javascript4 1
Return Value
Type Description
RegExp Object
A regular expression is an object that describes a pattern of characters.
Regular expressions are used to perform pattern-matching and "search-and-
replace" functions on text.
Syntax
/pattern/modifiers;
Example:
var pattern = /hello/i;
var str=”hellow world”;
var result=pattern.test(str);
// result will be true;
Modifiers
Modifiers are used to perform case-insensitive and global searches:
Modifier Description
Brackets
Brackets are used to find a range of characters:
Expression Description
[abc] Find any character between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find any character between the brackets (any digit)
[^0-9] Find any character NOT between the brackets (any non-digit)
Metacharacters
Metacharacters are characters with a special meaning:
Metacharacter Description
\d Find a digit
Example:
var pattern=/^h/g;
var str='hellow world';
var result=pattern.test(str);
//result=true
Example of String starts with capital-letter and end with digit validation
var result;
var result;
var num1 = "Ram9";
var startpatt =/^[A-Z]/;
var endpatt =/[0-9]$/;
var startresult = startpatt.test(num1);
var endresult = endpatt.test(num1);
if(startresult==true && endresult==true){
result=true;
}
else{
reslut=false;
}
//result=true;