`

JavaScript Regex

阅读更多

1.10 JavaScript

JavaScript introduced Perl-like regular expression support with Version 1.2. This reference covers Version 1.5 as defined by the ECMA standard. Supporting implementations include Microsoft Internet Explorer 5.5+ and Netscape Navigator 6+. JavaScript uses a Traditional NFA match engine. For an explanation of the rules behind an NFA engine, see Section 1.2 .

1.10.1 Supported Metacharacters

JavaScript supports the metacharacters and metasequences listed in Table 1-41 through Table 1-45 . For expanded definitions of each metacharacter, see Section 1.2.1 .

Table 1-41. Character representations

Sequence

Meaning

\0

Null character, \x00 .

\b

Backspace, \x08 , supported only in character class.

\n

Newline, \x0A .

\r

Carriage return, \x0D .

\f

Form feed, \x0C .

\t

Horizontal tab, \x09 .

\t

Vertical tab, \x0B .

\x hh

Character specified by a two-digit hexadecimal code.

\u hhhh

Character specified by a four-digit hexadecimal code.

\c char

Named control character.

Table 1-42. Character classes and class-like constructs

Class

Meaning

[...]

A single character listed or contained within a listed range.

[^...]

A single character not listed and not contained within a listed range.

.

Any character except a line terminator, [^\x0A\x0D\u2028\u2029] .

\w

Word character, [a-zA-Z0-9_] .

\W

Non-word character, [^a-zA-Z0-9_] .

\d

Digit character, [0-9] .

\D

Non-digit character, [^0-9] .

\s

Whitespace character.

\S

Non-whitespace character.

Table 1-43. Anchors and other zero-width tests

Sequence

Meaning

^

Start of string, or after any newline if in multiline match mode, /m .

$

End of search string or before a string-ending newline, or before any newline if in multiline match mode, /m .

\b

Word boundary.

\B

Not-word-boundary.

(?= ... )

Positive lookahead.

(?! ... )

Negative lookahead.

Table 1-44. Mode modifiers

Modifier

Meaning

m

^ and $ match next to embedded line terminators.

i

Case-insensitive match.

Table 1-45. Grouping, capturing, conditional, and control

Sequence

Meaning

( ... )

Group subpattern and capture submatch into \1 ,\2 ,... and $1 , $2 ,....

\ n

In a regular expression, contains text matched by the n th capture group.

$ n

In a replacement string, contains text matched by the n th capture group.

(?:...)

Group subpattern, but do not capture submatch.

... |...

Try subpatterns in alternation.

*

Match 0 or more times.

+

Match 1 or more times.

?

Match 1 or 0 times.

{ n }

Match exactly n times.

{ n ,}

Match at least n times.

{ x ,y }

Match at least x times but no more than y times.

*?

Match 0 or more times, but as few times as possible.

+?

Match 1 or more times, but as few times as possible.

??

Match 0 or 1 times, but as few times as possible.

{ n }?

Match at least n times, but as few times as possible.

{ x ,y }?

Match at least x times, no more than y times, and as few times as possible.

1.10.2 Pattern-Matching Methods and Objects

JavaScript provides convenient pattern-matching methods in String objects, as well as a RegExp object for more complex pattern matching. JavaScript strings use the backslash for escapes, and therefore any escapes destined for the regular expression engine should be double escaped (e.g., "\\w " instead of "\w "). You can also use the regular expression literal syntax, / pattern/img .

String

Strings support four convenience methods for pattern matching. Each method takes a pattern argument, which may be either a RegExp object or a string containing a regular expression pattern.

Methods

search( pattern )

Match pattern against the string returning either the character position of the start of the first matching substring or -1 .

replace( pattern , replacement )

The replace( ) method searches the string for a match of pattern and replaces the matched substring with replacement . If pattern has global mode set, then all matches of pattern are replaced. The replacement string may have $ n constructs that are replaced with the matched text of the n th capture group in pattern .

match( pattern )

Match pattern against the string returning either an array or -1 . Element 0 of the array contains the full match. Additional elements contain submatches from capture groups. In global (g ) mode, the array contains all matches of pattern with no capture group submatches.

split( pattern , limit )

Return an array of strings broken around pattern . If limit , the array contains at most the first limit substrings broken around pattern . If pattern contains capture groups, captured substrings are returned as elements after each split substring.

RegExp

Models a regular expression and contains methods for pattern matching.

Constructor

new RegExp( pattern , attributes )
/ pattern /attributes

RegExp objects can be created with either the RegExp( ) constructor or a special literal syntax /.../ . The parameter pattern is a required regular expression pattern, and the parameter attributes is an optional string containing any of the mode modifiers g , i , or m . The parameter pattern can also be a RegExp object, but the attributes parameter then becomes required.

The constructor can throw two expceptions. SyntaxError is thrown if pattern is malformed or if attributes contains invalid mode modifiers. TypeError is thrown if pattern is a RegExp object and the attributes parameter is omitted.

Instance properties

global

Boolean, if RegExp has g attribute.

ignoreCase

Boolean, if RegExp has i attribute.

lastIndex

The character position of the last match.

multiline

Boolean, if RegExp has m attribute.

source

The text pattern used to create this object.

Methods

exec( text )

Search text and return an array of strings if the search succeeds and null if it fails. Element 0 of the array contains the substring matched by the entire regular expression. Additional elements correspond to capture groups.

If the global flag (g ) is set, then lastIndex is set to the character position after the match or zero if there was no match. Successive exec( ) or test( ) calls will start at lastIndex . Note that lastIndex is a property of the regular expression, not the string being searched. You must reset lastIndex manually if you are using a RegExp object in global mode to search multiple strings.

test( text )

Return true if the RegExp object matches text . The test( ) method behaves in the same way as exec( ) when used in global mode: successive calls start at lastIndex even if used on different strings.

1.10.3 Examples

Example 1-28. Simple match
//Match Spider-Man, Spiderman, SPIDER-MAN, etc.
    var dailybugle = "Spider-Man Menaces City!";

    //regex must match entire string
    var regex = /spider[- ]?man/i;
  
    if (dailybugle.search(regex)) {
      //do something
    }

Example 1-29. Match and capture group
//Match dates formatted like MM/DD/YYYY, MM-DD-YY,...
    var date = "12/30/1969";
    var p = 
      new RegExp("(\\d\\d)[-/](\\d\\d)[-/](\\d\\d(?:\\d\\d)?)");

    var result = p.exec(date);
    if (result != null) {
      var month = result[1];
      var day   = result[2];
      var year  = result[3];

Example 1-30. Simple substitution
//Convert <br> to <br /> for XHTML compliance
    String text = "Hello world. <br>";
    
    var pattern = /<br>/ig;

    test.replace(pattern, "<br />");

Example 1-31. Harder substitution
//urlify - turn URL's into HTML links
   var text = "Check the website, http://www.oreilly.com/catalog/repr.";
   var regex =                                                
        "\\b"                       // start at word boundary
     +  "("                         // capture to $1
     +  "(https?|telnet|gopher|file|wais|ftp) :"
                                    // resource and colon
     +  "[\\w/\\#~:.?+=&%@!\\-]+?"  // one or more valid chars
                                    // take little as possible
      +  ")"                                                               
     +  "(?="                       // lookahead
     +  "[.:?\\-]*"                 // for possible punct
     +  "(?:[^\\w/\\#~:.?+=&%@!\\-]"// invalid character
     +  "|$)"                       // or end of string  
     +  ")";

    text.replace(regex, "<a href=\"$1\">$1</a>");

1.10.4 Other Resources

  • JavaScript: The Definitive Guide , by David Flanagan (O'Reilly), is a reference for all JavaScript, including regular expressions.

分享到:
评论

相关推荐

    Ext 的正则表达式全

    ```javascript regex:/^\w+$/, ``` - 说明:此正则表达式用于验证输入字段必须包含至少一个字母数字字符或者下划线。 - 示例:`"abc"`, `"123"`, `"_test"` 都是合法的。 2. **验证只包含字母数字和下划线的...

    soloreg:JavaScript Regex在线编辑器

    JavaScript正则表达式(Regex)是编程语言中的一个重要部分,用于处理字符串的模式匹配和查找。"soloreg"是一个JavaScript编写的在线正则表达式编辑器,它为开发者提供了一个方便的平台来测试、调试和学习正则表达式...

    regex-engine:用JavaScript编写的简单回溯正则表达式引擎

    JavaScript中的简单Regex引擎(回溯算法) 注意:由于详细记录了源代码,因此本README相对简短。 这是JavaScript中正则表达式引擎的简单实现,该引擎使用传统的“回溯”算法将正则表达式模式与字符串进行匹配。 ...

    check-password-strength:一个简单的npm软件包,用于检查特定密码短语的密码强度。 基于Javascript RegEx的密码强度检查器

    -save 设置和基本用法const passwordStrength = require('check-password-strength')console.log(passwordStrength('asdfasdf').value)// Weak (It will return weak if the value doesn't match the RegEx ...

    SRLJavaScript实现。-JavaScript开发

    Simple Regex的SRL-JavaScript JavaScript实现由于JavaScript regex引擎,因此与Simple Regex支持有所不同,因为使用CODE SRL的Simple RegexJavaScript JavaScript实现分配捕获名称由于JavaScript regex引擎,与简单...

    regexpal正则实时匹配工具(基于javascript开发)

    regexpal正则实时匹配工具(基于javascript开发),无需安装,无需任何平台支撑,体积小,功能实用,使用普通浏览器就能打开. 基于javascript并应用ajax技术开发的正则表达式匹配检测工具. 正则表达式书写利器. 实时检测...

    js regex tester

    "js regex tester" 是一个专为JavaScript正则表达式设计的测试工具,它简化了这个过程,帮助开发者快速验证和优化他们的正则表达式。这个工具提供了一个友好的界面,可以即时反馈正则表达式的匹配结果,从而提高开发...

    JavaScript_RegExr是一个基于HTMLJS的工具,用于创建、测试和学习正则表达式.zip

    在JavaScript中,正则表达式(Regular Expression,简称Regex)是一种强大的文本处理模式,用于匹配、查找、替换和提取字符串中的特定模式。 正则表达式的核心概念包括: 1. **字符类**:例如[\d\D],用来匹配任何...

    regular-expressions-cheatsheet:在JavaScript中使用正则表达式的简明速查表

    在JavaScript中使用正则表达式的简明速查表 匹配 分组和捕获 标志 参考资料和工具 注意事项 遵循我的思维模式。 故意不全面。 仅包括语法和我实际使用的API的一部分。 某些概念定义不准确。 (例如,某些定义...

    Regex测试工具

    正则表达式(Regex)是计算机编程中一种强大的文本处理工具,它允许通过模式匹配来查找、替换或提取文本中的特定字符串。在多种编程语言中,如Java和JavaScript,正则表达式都被广泛使用,但其语法复杂,初学者往往...

    JS cookie Java cookie regex 整理结果

    JavaScript(JS)Cookie和Java Cookie是Web开发中用于存储客户端数据的两种常见方式,而正则表达式(Regex)在处理字符串和数据匹配时扮演着重要角色。下面将详细阐述这三个知识点及其应用。 首先,JavaScript ...

    RegExr.air

    5. 代码导出:RegExr.air支持将正则表达式导出为多种编程语言的代码片段,如JavaScript、Java、PHP等,方便在实际项目中使用。 二、使用RegExr.air学习正则表达式 1. 初步了解:通过RegExr.air提供的例子,用户...

    RegexTester

    2. **多种正则表达式语法支持**:RegexTester通常会支持多种正则表达式语法,如Perl兼容(PCRE)、JavaScript和.NET等,满足不同平台和语言的需求。 3. **预编译和性能测试**:工具可能提供预编译正则表达式的功能...

    正则表达式工具(检测_保存) RegExr.rar

    2. **多种模式**:RegExr支持多种正则表达式引擎的语法,如ECMAScript(JavaScript)、Perl、PCRE(PHP)等,让用户能够在不同编程语言环境下编写兼容的正则表达式。 3. **功能完备**:RegExr包含了丰富的元字符、...

    正则表达式测试工具RegexTester 中文版

    7. **兼容性**:RegexTester通常会支持多种正则表达式引擎,包括Perl、JavaScript、PCRE(Perl兼容正则表达式)等,这样你可以在一个环境中测试不同语言下的正则表达式。 8. **调试功能**:对于复杂的正则表达式,...

    RegexTester 正则表达式测试工具

    2. **多模式匹配**:支持多种正则表达式语法,包括ECMAScript (JavaScript)、Perl、PCRE (PHP) 和 .NET 等,满足不同语言环境的需求。 3. **分组与捕获**:RegexTester可以显示分组匹配的结果,这对于理解复杂的...

    正则表达式工具RegexTester

    3. **多种正则语法支持**:RegexTester兼容多种正则表达式语法,包括Perl、Java、.NET、JavaScript等,满足不同编程环境的需求。 4. **测试用例管理**:你可以输入多行文本作为测试用例,测试正则表达式在不同情况...

    cspell-grammar:tmLanguage cSpell语法解析器

    由于JavaScript regex引擎的限制,它仅支持的子集。 使用 regex引擎提供了更广泛的支持。 由于可移植性原因,Cspell不使用vscode-textmate或Oniguruma。 目的是利用定义良好的语法来帮助进行拼写检查过程。 这个...

    Regex Match Tracer 正则表达式 测试工具

    3. **多种正则引擎支持**:Regex Match Tracer可能支持多种正则表达式引擎,如Perl兼容(PCRE)、ECMAScript(JavaScript风格)等,以适应不同编程语言和环境的需求。 4. **代码生成**:该工具可能还提供了代码生成...

Global site tag (gtag.js) - Google Analytics