Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

正则表达式之断言 #28

Open
LiuL0703 opened this issue Jul 7, 2019 · 0 comments
Open

正则表达式之断言 #28

LiuL0703 opened this issue Jul 7, 2019 · 0 comments
Labels

Comments

@LiuL0703
Copy link
Owner

LiuL0703 commented Jul 7, 2019

正则表达式--断言

Positive Lookbehind

  • (?<=) #断言要匹配的文本前缀

let str = 'qwert&code=1243&qerfvs=6577';
str.replace(/(?<=code=)\d+/,'4321');     // "qwert&code=4321&qerfvs=6577"

Negative Lookbehind

  • (?<!) #断言位置不能匹配的文本前缀

    "(?<!(T|t)he\s)(cat)"  => The cat sat on cat.

Positive Lookahead

  • (?=) #断言要匹配的文本后缀

    "(T|t)he(?=\sfat)" => The fat cat sat on the mat.
    // 匹配 紧跟fat的 The或the 

Negative Lookahead

  • (?!) #断言位置不能匹配的文本后缀

    "(T|t)he(?!\sfat)" => The fat cat sat on the mat.
    // 匹配文本后面不紧跟fat的The 或the

总结

  • X(?=Y) Positive lookahead X if followed by Y
  • X(?!Y) Negative lookahead X if not followed by Y
  • (?<=Y)X Positive lookbehind X if after Y
  • (?<!Y)X Negative lookbehind X if not after Y
@LiuL0703 LiuL0703 added the RegExp label Jul 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant