RegExp
The literal for a regular expression object is / ~ /. (Not both "/ ~ /" and "/ ~ /") Regular expression objects have test() and exec() methods. The test() returns a boolean value by determining whether the string passed as an argument matches the regular expression. The exec() finds and returns strings that match the regular expression in its argument.
var regExp = /Java/; var testStr = "www.java-school.net is the best site to learn Java"; var retArr = regExp.exec(testStr); alert(retArr[0]);
Flag
The string after the / at the end of the regular expression object is a flag.
i
Flag i means not case sensitive.
var regExp = /Java/i; var testStr = "www.java-school.net is the best site to learn Java"; var retArr = regExp.exec(testStr); alert(retArr[0]);
g
Flag g remembers the last matching location to make the next exec() find the following matching location from the previous matching location.
var regExp = new RegExp('Java', 'gi'); // /Java/gi var testStr = "www.java-school.net is the best site to learn Java"; var retArr = regExp.exec(testStr); retArr = regExp.exec(testStr); alert(retArr[0]);
The character between / and /
Number of times | |
---|---|
* | 0 times or more |
+ | More than once |
? | 0 or 1 time |
. | Exactly once |
{} | Curly braces are used to specify the number of times a character is repeated.(s{2} means ss) |
var regExp = /Java-*/gi; var testStr = "www.java-school.net is the best site to learn Java"; var retArr = regExp.exec(testStr); retArr = regExp.exec(testStr); alert(retArr[0]);
The combination of \ followed by a regular character acts as a special promised character. | |
---|---|
\w | It means a word, exactly alphabets, numbers and _. |
\W | Opposite \w |
\d | Numbers |
\D | Opposite \d, \D* means zero or more characters. |
\s | Space character |
The combination of \ followed by a special character means a character itself. | |
---|---|
\* | * is a special character that means many, but \* denotes the asterisk itself. |
var regExp = /\s\*/g; var testStr = "www.java-school.net *is *the *best *site *to *learn *JAVA"; var retStr = testStr.replace(regExp,'-'); alert(retStr);
The following is a form tag for membership.
<form id="signUp" action="signUp" method="post" onsubmit="return check()"> Name <input type="text" name="name" /> <!-- omit --> </form>
Let's write JavaScript code to check that the name-value consists only of whitespace characters.
function check() { var regExp = /\s/g; var form = document.getElementById("signUp"); var name = form.name.value; name = name.replace(regExp,""); if (name.length == 0) { alert("Invalid Name"); return false; } return true; }
Start (^) and End ($) | |
---|---|
^ |
It means the starting position of the string. /^JAVA/ finds "JAVA" at the beginning of the line. |
$ |
It means the ending position of the string. /school$/ finds "school" at the end of the line. |
If you want to match multiple characters, list them in [] | |
---|---|
[a-zA-Z] | matches an alphabet letter or alphabet strings |
[0-9] | matches a number or numbers |
^ in [] |
means to exclude something. [^0-9] is the same as \D |
() | Characters that match the pattern enclosed in parentheses are put into the array one after the other. You can get the stored string by $1, $2, etc. |
---|---|
| |
It means or. a|b matches a or b a|b|c matches a or b or c |
Several sites provide helpful regular expressions. One of them is http://regexlib.com. Let's create a JavaScript function that checks if the email and date are valid.
var emailRegExp = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/; var dateRegExp = /^\d{4}\/\d{2}\/\d{2}/; var email = "[email protected]"; var signUpDate = "2018/4/21"; var check = emailRegExp.test(email); if (check) { alert("Valid email"); } else { alert("Invalid email"); } check = dateRegExp.test(signUpDate); if (check) { alert("Valid registration date"); } else { alert("InValid registration date"); }References