Match 10th,2009

March 10th,2009,Tuesday Sunny
When i was walking on the road to my company off subway line 1.
I met a colleague who i just have seen yesterday, he active said hello to me ,and talked with me .He is a very conversable,but i'm not.
They say that a good soft engineer not only can code ,but also can intercommunion with others .
But i'm not a good speaker , i must to correct this disadvantage, i will do my best to speak more ,clearly and efficiently .
Though perseverance is boring and difficult , i will insist.
### JavaScript `match` 函数的用法 #### 方法签名 字符串对象的方法之一是 `match()`,用于检索正则表达式的匹配项。该方法执行的是一个全局或局部文本搜索,以找到由指定模式所匹配的子串;它返回的结果是一个数组,其中包含了所有成功匹配的结果[^1]。 ```javascript string.match(regexp) ``` - 参数 `regexp`: 正则表达式对象。如果传入的是字符串,则会自动转换成新的 RegExp 对象。 - 返回值: 如果未找到任何匹配项,则返回 null;否则返回一个数组,第一个元素为首次匹配成功的整个结果,后续元素则是捕获组的内容(如果有)。当设置了全局标志 'g' 或者 sticky 标志 'y' 后,仅返回完整的匹配而不会包含分组信息。 #### 使用实例 ##### 单次匹配 下面的例子展示了如何利用简单的正则表达式来查找单词 "hello": ```javascript const text = "Say hello world!"; const pattern = /hello/; let result = text.match(pattern); console.log(result); // ["hello", index: 4, input: "Say hello world!", groups: undefined] ``` ##### 多次匹配 为了获取所有的匹配项而不是第一次出现的位置,可以在正则表达式中加入全局标记 g: ```javascript const paragraph = "The rain in SPAIN stays mainly in the plain."; const regexGlobal = /ain/g; let matches = paragraph.match(regexGlobal); console.log(matches); // ["ain", "ain", "ain"] ``` ##### 获取更多信息 除了基本的匹配外,还可以得到更多关于每次匹配的信息,比如索引位置等: ```javascript const str = "Is this all there is?"; const reWithGroups = /(th)(is)/gi; var arr = str.match(reWithGroups); if (arr !== null){ console.log(`Found ${arr.length} matches:`); for(var i=0;i<arr.length;i++){ console.log(`${i}: "${arr[i]}"`); } } // Found 2 matches: // 0: "This" // 1: "is" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值