国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

XRegExp 0.2: Now With Named Capture

2024-05-06 14:18:47
字體:
供稿:網(wǎng)友

Update: A beta version of XRegExp 0.3 is now available as part of the RegexPal download package.

JavaScript's regular expression flavor doesn't support named capture. Well, says who? XRegExp 0.2 brings named capture support, along with several other new features. But first of all, if you haven't seen the previous version, make sure to check out my post on XRegExp 0.1, because not all of the documentation is repeated below.

Highlights

Comprehensive named capture support (New) Supports regex literals through the addFlags method (New) Free-spacing and comments mode (x) Dot matches all mode (s) Several other minor improvements over v0.1

Named capture

There are several different syntaxes in the wild for named capture. I've compiled the following table based on my understanding of the regex support of the libraries in question. XRegExp's syntax is included at the top.

LibraryCaptureBackreferenceIn replacementStored at
XRegExp(<name>…)/k<name>${name}result.name
.NET(?<name>…)
(?'name'…)
/k<name>
/k'name'
${name}Matcher.Groups('name')
Perl 5.10 (beta)(?<name>…)
(?'name'…)
/k<name>
/k'name'
/g{name}
$+{name}??
Python(?P<name>…)(?P=name)/g<name>result.group('name')
PHP preg (PCRE)(.NET, Perl, and Python styles)$regs['name']$result['name']

No other major regex library currently supports named capture, although the JGsoft engine (used by products like RegexBuddy) supports both .NET and Python syntax. XRegExp does not use a question mark at the beginning of a named capturing group because that would prevent it from being used in regex literals (JavaScript would immediately throw an "invalid quantifier" error).

XRegExp supports named capture on an on-request basis. You can add named capture support to any regex though the use of the new "k" flag. This is done for compatibility reasons and to ensure that regex compilation time remains as fast as possible in all situations.

Following are several examples of using named capture:

// Add named capture support using the XRegExp constructorvar repeatedWords = new XRegExp("http://b (<word> //w+ ) //s+ //k<word> //b", "gixk");// Add named capture support using RegExp, after overriding the native constructorXRegExp.overrideNative();var repeatedWords = new RegExp("http://b (<word> //w+ ) //s+ //k<word> //b", "gixk");// Add named capture support to a regex literalvar repeatedWords = //b (<word> /w+ ) /s+ /k<word> /b/.addFlags("gixk");var data = "The the test data.";// Check if data contains repeated wordsvar hasDuplicates = repeatedWords.test(data);// hasDuplicates: true// Use the regex to remove repeated wordsvar output = data.replace(repeatedWords, "${word}");// output: "The test data."            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 承德市| 藁城市| 潼南县| 东乡县| 乌拉特中旗| 洛宁县| 九龙县| 凤冈县| 瑞安市| 文山县| 深水埗区| 镇赉县| 北流市| 中牟县| 永善县| 莱芜市| 西充县| 宜春市| 蒙山县| 芦山县| 弥勒县| 上饶市| 渭源县| 漳州市| 许昌市| 盘山县| 化德县| 奉新县| 米脂县| 松江区| 江口县| 凌源市| 甘肃省| 冷水江市| 云和县| 逊克县| 闵行区| 中方县| 绥滨县| 文昌市| 昆山市|