match()方法在字符串中搜索與正則表達(dá)式的匹配項(xiàng),然后將匹配項(xiàng)作為Array對(duì)象返回。
如果正則表達(dá)式包含g
修飾符(全局搜索),則該方法將返回一個(gè)包含所有匹配的子字符串的Array。
如果正則表達(dá)式不包含g
修飾符(全局搜索),則該方法將僅返回字符串中的第一個(gè)匹配項(xiàng)。
如果沒(méi)有匹配項(xiàng),則該方法返回null。
您可以在RegExp教程和RegExp對(duì)象參考中了解有關(guān)正則表達(dá)式的更多信息。
string.match(regex)
var str = 'POLLUTION: Air Pollution is introduction of chemicals to the atmosphere'; var reg = str.match(/ion/g);測(cè)試看看?/?
所有瀏覽器都完全支持match()方法:
Method | ![]() | ![]() | ![]() | ![]() | ![]() |
match() | 是 | 是 | 是 | 是 | 是 |
參數(shù) | 描述 |
---|---|
regex | 正則表達(dá)式對(duì)象 |
返回值: | 一個(gè)包含匹配項(xiàng)的數(shù)組,每個(gè)匹配項(xiàng)一項(xiàng),如果找不到匹配項(xiàng),則為null |
---|---|
JavaScript版本: | ECMAScript 1 |
以下示例演示了將全局和忽略大小寫修飾符與match()一起使用:
var str = 'POLLUTION: Air Pollution is introduction of chemicals to the atmosphere'; var reg = str.match(/ion/gi);測(cè)試看看?/?
計(jì)算字符串中的元音數(shù)量:
在輸入字段中輸入一些文本以顯示元音數(shù)量:
語(yǔ)音: