Javascript has built-in support for regular expression in almost all browsers. This means you can use regular expression at client side too. You can use to validate the user’s input data at client side itself to reduce the server side processing, however you may want to check those at server side too for security problems.
To use regular expression in javascript you can use the RegExp object
1 2 3 | var myRegExp = new RegExp("[a-zA-z]+"); |
Or you can directly creates its instance by using special syntax
1 2 3 | var myRegExp = /[a-zA-z]+/ |
To test a string against a RegExp
1 2 3 4 5 6 7 8 9 10 11 | var myRegExp = /[a-zA-z]+/; if(myRegExp.test("codeexample")) { // Success } else { // Fail } |
If you want to extract multiple matched from the input string
1 2 3 4 5 6 7 8 9 | var myRegExp = /[a-zA-z]+/; var inputString = "sample"; var myMatches = document.frm1.txt1.value.match(myRegExp); for(var i=0; i<myMatches.length; i++) { alert(myMatches[i]); } |