gpt4 book ai didi

javascript - javascript 中的 '/' 是如何工作的?

转载 作者:行者123 更新时间:2023-11-30 10:34:51 25 4
gpt4 key购买 nike

这怎么可能?

var re = /\w+/;

我从未在任何其他语言中看到过这样的东西。这是语言语法的一部分还是其他?前导 '/' 是我不明白的,当它用 "\w+/"得到它时,js 到底做了什么?

最佳答案

它是一个正则表达式字面量。

From the MDN docs

You construct a regular expression in one of two ways:

Using a regular expression literal, as follows:

var re = /ab+c/; 

Regular expression literals provide compilation of the regular expression when the script is evaluated. When the regular expression will remain constant, use this for better performance.

Calling the constructor function of the RegExp object, as follows: 1

var re = new RegExp("ab+c");

Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

关于javascript - javascript 中的 '/' 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14650718/

25 4 0
文章推荐: javascript - 打开前 Jquery UI Accordion 折叠
文章推荐: javascript - 取消分配 Object3D
文章推荐: javascript - 捕获