Regular Basics

Some basic knowledge of regular

Create a regular

  • via constructorconst pattern = new RegExp(pattern,modifiers)

    • pattern: Matching string form, can have variables

    • modifiers: matching pattern,g(全局),i(忽略大小写),u(多行)

  • Literal form:const pattern = /pattern/g

    • pattern: matching content

regular method

  • reg.test(str): Check if a string has something to match, return a boolean value

  • reg.exec(str): This method is designed specifically for capturing groups (each parenthesis is a capturing group)

    • Parameters: the string to match

    • Return value: The returned value is an array. Returns null if no match

    • A note on returning an array of values:

      • It is indeed an instance of Array.

      • But this array has two extra properties: index and input

      • index: indicates the index of the matched string in the source string

      • input: Indicates the matching source string.

      • The first item of the array is the string that matches the entire pattern, the other items are the strings that match the capturing groups in the pattern

      • If there is no capturing group, there is only the first item in the array

Some rules for regular expressions

  • /[0-9a-zA-Z]/: matches []any character inside

  • /(abc|def)/: matches |one of the

  • /[^0-9]/: matches non-digits

  • \d: matches numbers

  • \D: matches non-digits

  • \w: matches letters and numbers and _

  • \W: matches not (letters and numbers and _)

  • \b: matches whitespace character

  • \n: matches a newline character

  • \t: matches tabs

  • \0: matches the null character

  • \r: matches carriage return characters

  • \s: matches whitespace, spaces, tabs and newlines

  • \S: matches non-whitespace characters

  • ^: start of line match

  • $: end-of-line match

  • ?: 0 or 1

  • *: 0 or more

  • +: at least one

  • {m}: m times

  • {m,n}: m to n times

  • {m,}: at least m times

String methods that support regular expressions

  • search: retrieve the index of the first occurrence that matches the regular expression

  • match: Find a match for one or more regular expressions. In fact, it is similar to the method of regular expressionsexec

  • replace(reg|str,str|fn)

    • When the second parameter is a string, the matched characters will be replaced. (Regular can be replaced globally)

    • When the second parameter is a function, a callback will be made for each character matched, and the first parameter of the callback is the matched character

      • When the first parameter is a string, the second parameter of the callback is the index of the matched character, and the third parameter is the entire character

      • When the second parameter is regular, if the matched character is not captured, it is the same as above. If there is capture, the captured item will be passed in as a parameter, and the parameters after capture are the same as above.

  • some examples

    • Get query parameters


      function getQueryByName(name) {const search = location.searchconst ret = search.match(new RegExp('[?&]'+name+'([^&]'*))return ret&&ret.length > 0 ? ret[1] : ''                         }
         
         
         

       

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324641679&siteId=291194637