JS basis - Regular

Regular Expressions

Creating regular expressions

  1. Use a regular expression literals
const regex = /^[a-zA-Z]+[0-9]*\W?_$/gi;
  1. Call the constructor RegExp object
const regex = new RegExp(pattern, [, flags])

Special characters

  • ^ Matches the start of input
  • $ Matches the end of input
  • * Zero or more times {0}
  • + 1 or more times {1}
  • ?
    • Zero or 1 {0,1}.
    • For the first assertion
    • If any quantifier * immediately, or {+ ,?} later, will make the quantifier becomes non-greedy
      • Of "123abc" with / \ d + / will return "123",
      • Use / \ d +? /, Then it will only match to "1."
  • . Matches any single character except a newline
  • (X) matches the 'x' and remember Matches
  • (:? X) matches the 'x' but does not remember a match
  • x (? = y) with 'x' only if 'x' followed by 'y'. This is called positive affirmation to find.
  • x (?! y) matches the 'x' only if 'x' is not followed behind 'y', this is called negative look forward.
  • x | y matches the 'x' or 'y'.
  • {N} n times
  • {N, m} matches at least n times, up to m times
  • [XYZ] Representative x or y or z
  • [^ Xyz] x or y or z are not
  • \ D digit
  • \ D non-digital
  • \ S whitespace characters, including spaces, tabs, page breaks and line breaks.
  • \ S non-whitespace characters
  • \ W word character (letters, numbers or underscores) [A-Za-z0-9_]
  • \ W non-word character. [^ A-Za-z0-9_]
  • \ 3 shows a third grouping
  • \ B word boundary
    • / \ Bm / matching "moon" in the get 'm';
  • \ B non-word boundary

Use regular expressions

  • performing a lookup exec RegExp matching method in the string, it returns an array (not match returns to null).
  • Test Method RegExp a test for a match in the string, which returns true or false.
  • performing a lookup match String matching method in the string, it returns an array or null when not matched.
  • String search in a test method for matching string, it returns to the location index matched, or -1 on failure.
  • replace a method of performing a lookup in String matching string, and used to replace the replace the substring matching string.
  • a split a regular expression or a character string separated by a fixed string, and stores the substring String methods to partition the array.

Exercise

Numbers match the end of

/\d+$/g

Uniform number of spaces
if the space, but the number of spaces in the string may be inconsistent, then by the number of spaces being unified into a.

let reg = /\s+/g
str.replace(reg, " ");

Determining if a string is composed of numbers

str.test(/^\d+$/);

Regular telephone numbers

  • Area Code Required 3-4 digits
  • After a code "-" is connected with a telephone number for the telephone number digits 7-8
  • The extension number of 3-4 digits, optional, but if the fill by the "-" and the phone number connected
/^\d{3,4}-\d{7,8}(-\d{3,4})?$/

Phone number regular expression
regular verification phone number, omit the leading 0, support 130-139,150-159. After it is determined omit the leading 0 is 11-bit.

/^0*1(3|5)\d{9}$/

Use regular expressions to realize delete spaces in the string

funtion trim(str) {
  let reg = /^\s+|\s+$/g
  return str.replace(reg, '');
}

Limiting the text box can only enter numbers and two decimal places, and so on

/^\d*\.\d{0,2}$/

Only lowercase letters and a decimal point, and colon, is a backslash (: ./)

/^[a-z\.:\/\\]*$/

Replace the contents of a decimal point before the specified content
such as: infomarket.php id = 197 replaced test.php id = 197??

var reg = /^[^\.]+/;
var target = '---------';
str = str.replace(reg, target)

Only regular expression matching Chinese

/[\u4E00-\u9FA5\uf900-\ufa2d]/ig

Returns the number of Chinese characters string
to remove non-Chinese characters, and then returns the length property.

function cLength(str){
  var reg = /[^\u4E00-\u9FA5\uf900-\ufa2d]/g;
  //匹配非中文的正则表达式
  var temp = str.replace(reg,'');
  return temp.length;
}

Regular expression matching IP address made the first three paragraphs
long match out of the last paragraph and replaced with an empty string on the line

function getPreThrstr(str) {
  let reg = /\.\d{1,3}$/;
  return str.replace(reg,'');
}

match

    versus
Content between

/<ul>[\s\S]+?</ul>/i

As a regular expression to get the filename
c: \ images \ tupian \ 006.jpg
may be directly under the root directory of the drive letter may also be under several layers directory, requirements to replace only the file name.
First, about the non-matching slash character 0 or more, and is about one or more diagonal lines.

function getFileName(str){
  var reg = /[^\\\/]*[\\\/]+/g;
  // xxx\ 或是 xxx/
  str = str.replace(reg,'');
  return str;
}

Absolute path disguised path
"http://23.123.22.12/image/somepic.gif" converted to: "/ image / somepic.gif"

var reg = /http:\/\/[^\/]+/;
str = str.replace(reg,"");

Regular user name
for the user name ,, registered user name can only use Chinese, English, numbers, underscores, 4-16 characters.

/^[\u4E00-\u9FA5\uf900-\ufa2d\w]{4,16}$/

English address matching
rules are as follows:
"Tap", "letter", "space", "comma", "Digital", but the beginning and end can not be any character except letters.

/^[a-zA-Z][\.a-zA-Z,0-9]*[a-zA-Z]$/

Regular price matches
the beginning of a number of digital bit, there may be a decimal point, you can have two digits after the decimal point.

/^\d+(\.\d{2})?$/

ID number matches
the ID number may be 15 or 18, which may be the last one X. Other all-digital

/^(\d{14}|\d{17})(X|x)$/

Word capitalized
every word the first word capitalized, other lowercase. The blue idea is converted to Blue Idea, BLUE IDEA also converted to Blue Idea

function firstCharUpper(str) {
  str = str.toLowerCase();
  let reg = /\b(\w)/g;
  return str.replace(reg, m => m.toUpperCase());
}

Regular verification date format
yyyy-mm-dd format
four digits, horizontal, 1 or 2 digits, then horizontal, and finally is 1 or 2 digits.

/^\d{4}-\d{1,2}-\d{1,2}$/

Remove the file extension of
www.abc.com/dc/fda.asp become www.abc.com/dc/fda

function removeExp(str) {
  return str.replace(/\.\w$/,'')
}

Verify mailbox regular expression
beginning must be one or more words or characters - plus @, and then another one or more words or characters -. Then point and word characters and "." - combination that can have one or
more combinations.

/^[\w-]+@\w+\.\w+$/

Regular closing determines whether the label
, for example: <img xxx = "xxx" tag is not closed;

P content, is also not closed tags.

There are two possible ways the closing tag, or

xxx

/<([a-z]+)(\s*\w*?\s*=\s*".+?")*(\s*?>[\s\S]*?(<\/\1>)+|\s*\/>)/i

Regular determines whether mixed numbers and letters
not less than 12, and must be mixed letters and numbers

/^(([a-z]+[0-9]+)|([0-9]+[a-z]+))[a-z0-9]*$/i

The Arabic replaced Chinese uppercase

function replaceReg(reg,str){
  let arr=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"];
  let reg = /\d/g;
  return str.replace(reg,function(m){return arr[m];})
}

Remove all the properties of the label

becomes no attribute

ideas: Non-matching attributes captured, capture the matching tag, the results using a capture replace string. Regular as follows:

/(<td)\s(?:\s*\w*?\s*=\s*".+?")*?\s*?(>)/

Guess you like

Origin www.cnblogs.com/nayek/p/11729916.html