Copyright statement: original works can be reproduced. During reprinting, you must mark the original publication, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://blog.csdn.net/mayongzhan-ma yongzhan, myz, mayongzhan
Address: http://www.thespanner.co.uk/2008/02/01/javascript-regular-expressions/
Ronald and I discussed the regular expressions in JavaScript and PHP. He used to use PHP preg and was unfamiliar with JavaScript Regular Expressions. So I shared my script knowledge.
First, the preg_match in PHP has the same function as the match in Javascript. They are similar except syntax. in JavaScript, match is a part of a string object (a string). The following describes the usage of match:
Alert ('test'. Match (/[A-Z]/)
You can also use the match on the sub-regular match, as shown below (I try to write these similar to PhP ):-
Pattern =/([A-Z] +) ([0-9] +) ([A-Z] + )/;
Subject = 'test1234test ';
Matches = subject. Match (pattern );
Matches = matches [1];
Match2 = matches [2];
Match3 = matches [3];
Alert (mattings );
Alert (match2 );
Alert (match3 );
You can also use Regexp to create a matching expression, which is conducive to passing variables to the matching expression "//"
A = 'a + ';
Alert (New regexp(acmd.exe C ('123aaaabcdef '));
The exec method can also be used by a matching expression like.
Alert (/[0-9] +/cmd.exe C ('12345abcdef '))
Javascript supports modifiers such as "g", "I", and "M", as follows:
Matches = 'ababababababa '. Match (/[a]/G );
Alert
Modify by planet PHP. For more information: the original (4722 bytes)
Ronald and I had a good conversation about Javascript Regular Expressions comparing them to PhP. he was having difficultly with the syntax because he was used to preg in PHP so I promised to share my knowledge gained from developing various online scripts.
First up preg_match in PHP can be achieved using the match function in Javascript, they are both very similar but it's just a matter of getting your head round the different syntax. match is part of the string object and here is how to use it :-
Alert ('test'. Match (/[A-Z]/)
You can match subpatterns like this (I 've tried to keep it close to PHP syntax as possible ):-
Pattern =/([A-Z] +) ([0-9] +) ([A-Z] + )/;
Subject = 'test1234test ';
Matches = subject. Match (pattern );
Matches = matches [1];
Match2 = matches [2];
Match3 = matches [3];
Alert (mattings );
Alert (match2 );
Alert (match3 );
You can also create matches using the Regexp object, this is useful for passing variables into the pattern which to my knowledge isn't possible with "//" syntax.
A = 'a + ';
Alert (New regexp(acmd.exe C ('123aaaabcdef '));
The exec method can also be called from "//" patterns.
Alert (/[0-9] +/cmd.exe C ('12345abcdef '))
Javascript supports the modifiers "G", "I" and "M", here's how to use them with "//" syntax :-
Matches = 'ababababababa '. Match (/[a]/G );
Alert
Truncated by planet PHP, read more at the original (another 4722 bytes)