Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the match function in JavaScript

Shulou Source: shulou.com Published: 2022-06-02 06:47:52 11月01日 Update

JavaScript match function how to use, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.

Function: Performs a lookup on a string using regular expression patterns and returns the results containing the lookup as an array

Function format: stringObj.match(rgExp) stringObj is required for string rgExp is required for regular expression

Return value: if it can match, return result array; if it cannot match, return null.

The copy code is as follows:

//Performs a lookup on a string using a regular expression pattern and returns the results containing the lookup as an array

var objStr=new String("Yue I love you till the end of my life! ");

var re=//wo(/w+)?/ g;//This expression is used to match a character represented by/w, the second character is o, followed by one or more characters + indicates the number of times the previous character matches, global match

var arr=objStr.match(re);//Call match method matches string and returns array if there is one, null if no result

[xss_clean]("string before match:"+objStr+"

");//output string before matching

if(arr!= null){//If the match succeeds, the arr array is not empty, and the loop outputs the result.

for(var i=0;i

Tags: Character string array result expression regular function code pattern and will help output clear successful representative global content function multiple for this Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Huawei Redmi Shulou Information Linux