我不明白方括号是什么意思

时间:2019-02-05 23:24:34

标签: javascript

我尝试在“ MND网络文档”上学习string.search,但我不明白console.log()部分中的方括号是什么意思。

请给我一些帮助的提示。如果有人可以向我展示一些示例,我会非常感谢您。

var paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
var regex = /[^\w\s]/g;
console.log(paragraph[paragraph.search(regex)]);

1 个答案:

答案 0 :(得分:0)

这是the search function的文档。

此处的括号用于在正则表达式搜索返回的索引处索引段落。正则表达式正在寻找不是空格或单词字符的任何字符。它在索引43处找到该点,因此paragraph.search(regex)返回43,该索引用于索引段落字符串(一个字符数组),并返回.

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const regex = /[^\w\s]/g;

console.log('index:', paragraph.search(regex));

console.log('char at index:', paragraph[paragraph.search(regex)]);