Abstract: Regular expression is a programmer’s necessary skills, do you want to learn more?
This article uses JavaScript’s exec method to test regular expressions.
For example, the regular expression /F.*g/ matches “strings that start with F and end with g”, so it matches “Hello, Fundebug!” The Fundebug, exec method returns an array whose first element is the matched substring.
/F.*g/.exec("Hello, Fundebug!") [0]
// 'Fundebug'
Copy the code
Non-greedy matching
By default, regular expression quantifiers ***, +,? , {}, are greedy matches, that is, matches as many characters ** as possible.
For example, the regular expression /.+\s/ matches “space-terminated strings”, We used it to match apple founder Steve Jobs’ famous quote from his Stanford University speech, “You time is limited, so don’t waste it living someone else’s life” :
/.+\s/.exec("You time is limited, so don’t waste it living someone else’s life.") [0]
// 'You time is limited, so don’t waste it living someone else’s '
Copy the code
. Can match any character, while + means one or more matches and is greedy, so /.+\s/ does not match until the last space.
When we use quantifiers, +,? , {} followed by? , you can achieve non-greedy matching, that is, matching as few characters as possible.
For example, the regular expression /.+? \s/ matches the first space character and ends:
/. +? \s/.exec("You time is limited, so don’t waste it living someone else’s life.") [0]
// 'You '
Copy the code
Forward positive search
Use the regular expression x(? =y), can match ‘x’ only if ‘x’ is followed by ‘y’. This is a bit convoluted, but in a nutshell, matches x followed by y, where both x and y represent regular expressions.
For blog, for example, the RabbitMQ introductory tutorial address “blog.fundebug.com/2018/04/20/…” To match fundebug, use /[a-z]+(? =\.com)/, matches “English words before.com”
/[a-z]+(? =\.com)/.exec("https://blog.fundebug.com/2018/04/20/rabbitmq_tutorial/") [0]
// 'fundebug'
Copy the code
Advertising: Free trial is welcomeFundebug, for you to monitor online code bugs, improve user experience ~
Positive negative search
Positive positive search corresponds to positive negative search, which uses regular expression x(? ! Y), can “match ‘x’ only if ‘x’ is not followed by ‘y'”.
For example, primary school students know that the value of PI is 3.1415926. Those who can’t can write it down like this: “There is a temple on the top of the mountain, and there is a pot of wine and a piece of meat in the temple.” How do you match the numbers after the decimal point? You can use /\d+(? ! \ \.) /, matches “digits without a decimal point” :
/\d+(? ! \.) /.exec("3.1415926") [0]
/ / '1415926'
Copy the code
Using the positive lookup mentioned earlier, we can match the number before the decimal point:
/\d+(? = \.) /.exec("3.1415926") [0]
/ / '3'
Copy the code
Multi-line matching
Here are the lyrics to Bob Dylan’s Forever Young:
May God bless and keep you always,
may your wishes all come true,
may you always do for others
and let others do for you.
may you build a ladder to the stars
and climb on every rung,
may you stay forever young,
forever young, forever young,
May you stay forever young.
Copy the code
How to match the lyrics that start with forever, forever Young, forever Young?
It would be wrong to write /^forever.+/ :
/^forever.+/.exec("May God bless and keep you always,\nmay your wishes all come true,\nmay you always do for others\nand let others do for you.\nmay you build a ladder to the stars\nand climb on every rung,\nmay you stay forever young,\nforever young, forever young,\nMay you stay forever young.") // nullCopy the code
Why is it wrong? Because ^ matches the beginning of the entire string, not the beginning of each line.
**/^forever.+/m** : **/^forever.+/m** : **/^forever.
/^forever.+/m.exec("May God bless and keep you always,\nmay your wishes all come true,\nmay you always do for others\nand let others do for you.\nmay you build a ladder to the stars\nand climb on every rung,\nmay you stay forever young,\nforever young, forever young,\nMay you stay forever young.")[0]
// 'forever young, forever young,'
Copy the code
Capturing parentheses
Using parentheses () in a regular expression, you can extract specific substrings from a string.
For example, Fundebug was officially launched on November 11, 2016. The time is “2016-11-11”. How can I extract the year, month, and day of Fundebug? As follows:
/(\d{4})-(\d{2})-(\d{2})/.exec("2016-11-11")
// [ '2016-11-11', '2016', '11', '11', index: 0, input: '2016-11-11' ]
Copy the code
It can be seen that the regular expressions in the three brackets match the date of year, month and day respectively, and the results are exec returning elements 1 to 3 in the array in turn.
reference
- MDN: regular expression
About Fundebug
Fundebug focuses on real-time BUG monitoring for JavaScript, wechat applets, wechat games, Alipay applets, React Native, Node.js and Java online applications. Since its launch on November 11, 2016, Fundebug has handled more than 1 billion error events in total, and paid customers include Google, 360, Kingsoft, Minming.com and many other brands. Welcome to try it for free!
Copyright statement
Reprint please indicate the author Fundebug and this article addresses: blog.fundebug.com/2018/05/02/…