Now you know that the RegExp is one helpful tool for you to match,replace and test strings,i learned that from ‘正则表达式30分钟入门教程‘, a very simple tutorial.
I will not tell that i can use it at my will, but To gain anything from them you still have to know the methods and usage of regular expressions.
1.RegExp.exec(string)
|
1 2 |
var match = /s(amp)le/i.exec("Sample text");
// match then contarins ["Sample", "amp"] |
2.RegExp.test(string)
|
1 2 |
var match = /sample/.test("Sample text");
//match then contains false |
3.String.match(pattern)
|
1 2 |
var str = "Watch out for the rock!".match(/r?or?/g);
//str then contains ["o","or","ro"] |
4.String.search(pattern)
|
1 2 |
var ndx = "Watch out for the rock!".search(/for/);
//ndx then contains 10 |
5.String.replace(pattern,string)
|
1 2 |
var str = "Liorean said: My name is Liorean!".replace(/Liorean/g,'Big Fat Dork');
//str then contains "Big Fat Dork said: My name is Big Fat Dork!" |
6.String.split(pattern)
|
1 2 |
var str = "I am confused".split(/\s/g);
//str then contains ["I","am","confused"] |
In the examples, you will find some through methods belonging to the String object, some through methods belonging to the RegExp object, whether the regular expression is declared through an object constructor or a literal makes no difference as to the usage.






I’m gone to inform my little brother, that he should also visit this blog on regular basis to take updated from newest reports.
I truly didn’t expect to see a post like Tips:javascript regular expressions methods and usage today. Very impressive. Reminds me of something I found out about wireless credit card service the other day.