官术网_书友最值得收藏!

  • JavaScript:Moving to ES2015
  • Ved Antani Simon Timms Narayan Prusty
  • 424字
  • 2021-07-09 19:07:35

Greedy and lazy quantifiers

All the quantifiers that we discussed so far are greedy. A greedy quantifier starts looking at the entire string for a match. If there are no matches, it removes the last character in the string and reattempts the match. If a match is not found again, the last character is again removed and the process is repeated until a match is found or the string is left with no characters.

The \d+ pattern, for example, will match one or more digits. For example, if your string is 123, a greedy match would match 1, 12, and 123. Greedy pattern h.+l would match hell in a string hello—which is the longest possible string match. As \d+ is greedy, it will match as many digits as possible and hence the match would be 123.

In contrast to greedy quantifiers, a lazy quantifier matches as few of the quantified tokens as possible. You can add a question mark (?) to the regular expression to make it lazy. A lazy pattern h.?l would match hel in the string hello—which is the shortest possible string.

The \w*?X pattern will match zero or more words and then match an X. However, a question mark after * indicates that as few characters as possible should be matched. For an abcXXX string, the match can be abcX, abcXX, or abcXXX. Which one should be matched? As *? is lazy, as few characters as possible are matched and hence the match is abcX.

With this necessary information, let's try to solve some common problems using regular expressions.

Removing extra white space from the beginning and end of a string is a very common use case. As a String object did not have the trim() method until recently, several JavaScript libraries provide and use an implementation of string trimming for older browsers that don't have the String.trim() method. The most commonly used approach looks something like the following code:

function trim(str) {
  return (str || "").replace(/^\s+|\s+$/g, "");
}
console.log("--"+trim("   test    ")+"--");
//"--test--"

What if we want to replace repeated whitespaces with a single whitespace?

re=/\s+/g;
console.log('There are    a lot      of spaces'.replace(re,' '));
//"There are a lot of spaces"

In the preceding snippet, we are trying to match one or more space character sequences and replacing them with a single space.

As you can see, regular expressions can prove to be a Swiss army knife in your JavaScript arsenal. Careful study and practice will be extremely rewarding for you in the long run.

主站蜘蛛池模板: 贵德县| 卓尼县| 定兴县| 常宁市| 双流县| 聂拉木县| 岚皋县| 云龙县| 中江县| 枣阳市| 双城市| 财经| 温宿县| 峨眉山市| 肥东县| 余姚市| 阜城县| 台南县| 永平县| 东乌珠穆沁旗| 玉门市| 日照市| 贵南县| 玉田县| 柘城县| 涡阳县| 叙永县| 南岸区| 门源| 虎林市| 夏津县| 江津市| 奇台县| 洪泽县| 佛冈县| 铁岭市| 达州市| 泰安市| 桐柏县| 文昌市| 宜宾市|