1 | <SCRIPT LANGUAGE="JavaScript1.2"> |
2 | re = /(\w+)\s(\w+)/; |
3 | str = "John Smith"; |
4 | newstr=str.replace(re, "$2, $1"); |
5 | document.write(newstr) |
6 | </SCRIPT> // Prints out Smith, John |
7 | Suppose re is a RegExp object and str a string, then: |
8 | function testinput(re, str){ |
9 | if (re.test(str)) |
10 | midstring = " contains "; |
11 | else |
12 | midstring = " does not contain "; |
13 | document.write (str + midstring + re.source); |
14 | } |
15 | Prints out a summary of matching result |