This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

Introduction to regular Expressions

  • A regular expression is a search pattern formed by a sequence of characters.
  • When you search for data in text, you can use search patterns to describe what you are looking for.
  • A regular expression can be a simple character or a more complex pattern.
  • Regular expressions can be used for all text search and text replacement operations.

Regular expressions are used in js

Syntax format

/ Regular expression body/modifier (optional)Copy the code

The modifier

Can be case insensitive in global search:

  • I: Performs case-insensitive matching.
  • G: Performs a global match (looks for all matches instead of stopping after finding the first match)
  • M: Perform multi-line matching.

Regular expression body

A literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters).

String method

  1. Search () method

Retrieves a substring specified in a string, or a substring that matches a regular expression, and returns the starting position of the substring.

var str = "study js"; 
console.log(str.search(/js/i)) / / 6
Copy the code
  1. The replace () method

Use to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.

var str = "study js"; 
console.log(str.replace(/js/i."css")) //study css
Copy the code

Regular expression method

  1. The test () method

The test() method is used to test whether a string matches a pattern, returning true if the string contains matching text and false otherwise.

var str = "study js"; 
var patt = /js/;
console.log(patt.test(str)) //true
Copy the code
  1. The exec () method

The exec() method is used to retrieve a match of a regular expression in a string, which returns an array containing the result of the match. If no match is found, the return value is null.

var str = "study js"; 
var patt = /js/;
console.log(patt.exec(str)) //["js", index: 6, input: "study js", groups: undefined]
Copy the code

practice

We often use regular expressions to validate the input character type of the control text box. Here are a few:

1. Enter only numbers and English:

<input onkeyup="value=value.replace(/[\W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" ID="Text1" NAME="Text1">
Copy the code

2. Can only enter numbers:

<input onkeyup="value=value.replace(/[^\d]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" ID="Text2" NAME="Text2">
Copy the code

3. Only full-angle:

<input onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))" ID="Text3" NAME="Text3">
Copy the code

4. Can only input Chinese characters:

<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))" ID="Text4" NAME="Text4">
Copy the code