String.prototype.match()
来源:互联网 发布:linux accept函数 编辑:程序博客网 时间:2024/05/16 17:26
Summary
The match()
method retrieves the matches when matching astring against a regular expression.
Syntax
str.match(regexp)
Parameters
regexp
- A regular expression object. If a non-RegExp object
obj
is passed, it is implicitly converted to aRegExp
by usingnew RegExp(obj)
.
Returns
array
- An
Array
containing the matched results ornull
if there were no matches.
Description
If the regular expression does not include the g
flag, returns the same result asRegExp.exec()
. The returned Array
has an extra input
property, which contains the original string that was parsed. In addition, it has anindex
property, which represents the zero-based index of the match in the string.
If the regular expression includes the g
flag, the method returns anArray
containing all matches. If there were no matches, the method returnsnull
.
See also: RegExp
methods
- If you need to know if a string matches a regular expression
RegExp
, usesearch()
. - If you only want the first match found, you might want to use
RegExp.exec()
instead.
Examples
Example: Using match()
In the following example, match()
is used to find 'Chapter'
followed by 1 or more numeric characters followed by a decimal point and numeric character 0 or more times. The regular expression includes thei
flag so that case will be ignored.
var str = 'For more information, see Chapter 3.4.5.1';var re = /(chapter \d+(\.\d)*)/i;var found = str.match(re);console.log(found);// logs ['Chapter 3.4.5.1', 'Chapter 3.4.5.1', '.1']// 'Chapter 3.4.5.1' is the first match and the first value // remembered from `(Chapter \d+(\.\d)*)`.// '.1' is the last value remembered from `(\.\d)`.
Example: Using global and ignore case flags with match()
The following example demonstrates the use of the global and ignore case flags withmatch()
. All letters A through E and a through e are returned, each its own element in the array.
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';var regexp = /[A-E]/gi;var matches_array = str.match(regexp);console.log(matches_array);// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
Specifications
The definition of 'String.prototype.match' in that specification.Standard ECMAScript 6 (ECMA-262)
The definition of 'String.prototype.match' in that specification.Draft
Browser compatibility
- Desktop
- Mobile
Firefox-specific notes
- Starting with Gecko 27 (Firefox 27 / Thunderbird 27 / SeaMonkey 2.24), this method has been adjusted to conform with the ECMAScript specification. When
match()
is called with a global regular expression, theRegExp.lastIndex
property (if specified) will be reset to0
(bug 501739).
See also
RegExp.prototype.exec()
RegExp.prototype.test()
- String.prototype.match()
- JS正则表达式exec()与String.prototype.match()的差异
- JS中string方法中常用方法之九:String.prototype.match()
- String.prototype
- BM string match algorithm
- string search and match
- binary string match
- systemverilog string match
- 686. Repeated String Match
- 686. Repeated String Match
- Leetcode | Repeated String Match
- String.prototype.trim
- String.prototype使用
- String.prototype.replaceAll
- String.prototype使用
- String.prototype.slice() 方法
- js String.prototype.trim()
- JavaScript String 对象 match() 方法
- iOS开发的一些奇谲巧技
- ActiveRecord测试
- C# JsonHelper
- iOS开发:开发者账号申请后真机调试详细步骤
- oracle 分析函数(笔记)
- String.prototype.match()
- java--------【对象之间的关系】
- Oracle中查询时候使index索引失效的限制条件
- 2015年IT变革与热点技术研讨会
- BZOJ 1965 AHOI2005 SHUFFLE 洗牌 数论
- NSURL的scheme, path和query等各接口输出结果
- 笔记:快速排序的java实现,递归的实现
- Unity手机开发优化
- 如何创意优质的广告语