A little note using regular expressions in JavaScript

Source: Internet
Author: User
Tags expression regular expression stdin

A few days ago in the gap time in reading mastering Regular expressions this book. It is a good book about regular expressions, and I should buy one. It is now temporarily borrowed from the Avik to buy the third edition of the Chinese version to read. The Chinese version of the translation is also good. There are a lot of places feel very wonderful translation, although immediately can guess the original is how to write, but read the Chinese do not feel a mouthful.

Now read to page 76th, chapter Two, the more introductory part. It used to be a regular expression, but while reading the book, it felt like there was a lot of traps in the past. And I never knew that there was Lookaround (Positive/negative lookahead/lookbehind) in regular expressions .... That's terrible. But it's not too late to learn.

The tricky part is that almost all of the code examples in chapter two are written in Perl, and I used to have little Perl code written by someone else. This is a taste of the power of a lot of Perl ah.

In simple terms, like this:

Perl code

1. $input = <STDIN>; # Get a line of input from stdin
2.chomp ($input); # cut the trailing newline character
3.
4.# verify the input as a decimal number
5.if ($input =~/^ ([-+]?[ 0-9]+ (?: \. [0-9]*)?] $/ ) {
6. # verified
7. $result = $input * 2;
8. print "input multiplied by 2 are: $result";
9.} else {
# Verification failed
print "Expecting a number, so I don ' t understand \" $input \ ". \ n";
12.}

It seems to me that the dynamic character of language and the graceful combination of regular expressions. ($input type can be automatically converted between string and number, and regular expressions can act on a string version of the type to check whether a single line of input is a valid digit.) )

Looking back, my initial exposure to regular expressions was in Java. Although the compiler theory class also talked about DFA/NFA and regular expressions, but at that time did not use the regular expression in C/s + +, but only to achieve a simple DFA.

Java to use regular expression is quite uncomfortable. The most painful is the problem of escaping characters. I was so unfamiliar with some of the predefined character groups used in regular expressions that I had to read Javadoc in the Java standard library to learn. Looking at Javadoc in the Java.util.regex.Pattern class, you can see a lot of predefined character groups related to the escape character, but it took me a long time to understand that the escape character in the Java itself string is not the same as the escape character in the regular expression-- They are superimposed. In order to express the \b in the regular expression, a string of "\\b" should be written in Java, in order to match a slash and period "\." And to write the regular expression \\\, in Java it has to be written as "\\\\\\." Results I often in a large number of "\" confused, in the end wrote how many "\" are countless ...

This escaped problem has been mitigated by exposure to C # with verbatim string and D with WYSIWYG string, but it would be more comfortable to write the literal of a regular expression instead of using a string to represent the regular expression. When reading some JavaScript related articles, I noticed that a lot of people mentioned "Perl-style regular expression notation" and looked it up and found that the operation/regex/modifier notation was really useful. But at that time is also casually wrote a few JavaScript small test play to play without a tube, also did not go to see the source of this notation--perl what it looks like. This time, remember the Perl-style regex in JavaScript.

Notes

There's a tutorial on JavaScript on the webref, but it's old, December 4, 1997 last edited. Although the article is older, the part of the regular expression is still available for reference; After all, ECMAScript has not officially released the updated version after V3. JavaScript 1.8 will follow Firefox 3.0 before it appears, and JavaScript 2.0 has to wait for ECMAScript to be finalized. Long AH. The technique of JavaScript 1.8 seems to be a bit of a record here and you have to look at it when you are free.

When I buy a new rhino book, I'll combine ECMA-262 to check if there's any updates.

The Mozilla MDC has regular expression documents that correspond to JavaScript 1.5 (with 1.8 updates).

Some introductions about Lookaround.

Let's take a look at some simple examples.

JS Code

1.<script>
2.var str = "<strong>text here</strong>123"
3.var regex =/^< ([^>]*) >.*<\/\1> (? =\d+)/
4.
5.//EXAMPLE 1
6.//validate a string with given regex, returning a Boolean value
7.var result = regex.test (str)
8.document.write (Result + "<br/>")//True
9.
10.//EXAMPLE 2
11.//match a string with given regex, returning the matches in an array
12.result =/(? =\w) (?: [^>]*) (? =\w)/.exec (str)
13.document.write (Result + "<br/>")
14.//text
15.
16.for (var i in result) {
document.write (i + ":" + result[i] + "<br/>")
18.}
19.//Input:text here123
20.//Index:8
21.//lastindex:13
22.//0:text
23.
24.//EXAMPLE 3
25.//match a string with given regex, returning the matches in an array
26.result = Str.match (regex)
27.for (var i in result) {
document.write (i + ":" + result[i] + "<br/>")
29.}
30.//Input: <strong>text here</strong>123
31.//index:0
32.//lastindex:26
33.//0: <strong>text here</strong>
34.//1:strong
35.
36.//EXAMPLE 4
37.//substitute a matched string with some other string
38.result = "Vaarbcaadbcae". Replace (/(a) + (?:. *) (BC)/gi, "_$2-$1_")
39.document.write (Result + "<br/>")
40.//V_bc-a_ae
41.
42.//EXAMPLE 5
43.//substitute a matched string with some other string
44.result = "Vaarbcaadbcae". Replace (/(a) + (?:. *) (BC)/GI, function (str, p1, p2) {
Return "_" + P2 + "+" + p1 + "_"
46.})
47.document.write (Result + "<br/>")
48.//V_bc+a_ae
49.</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.