JavaScript regular expressions
JavaScript Regular Expression
Regular Expression (English: Regular Expression, often abbreviated as regex, regexp or RE in code) uses a single string to describe and match a series of matches A string search pattern for a certain syntax rule.
Search mode can be used for text search and text replacement.
What is a regular expression?
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 want to query.
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.
Syntax
/pattern/modifiers;
Use string methods
In JavaScript, regular expressions are commonly used in two string methods: search() and replace().
search() method is used to retrieve a specified substring in a string, or to retrieve a substring that matches a regular expression, and returns the starting position of the substring.
replace() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.
search() method uses regular expressions
搜索字符串 "School", 并显示匹配的起始位置:点我function myFunction() { var str = "go home!"; var n = str.search(/home/i); document.getElementById("demo").innerHTML = n; }