javascript正则表达式 - 在我的情况下我可以做什么?

时间:2021-03-23 03:59:46

what I would like to do is-

我想做的是 -

var t = "search value"

regular expression is -

正则表达式是 -

[\,,\s]t[\,,\s]+

How can I put it in here-

我怎么能把它放在这里 -

var re = new RegExp(t, 'g');

1 个解决方案

#1


1  

Solution

You have passed t as the first parameter. It should not be the search value, but the pattern

您已将t作为第一个参数传递。它不应该是搜索值,而是模式

According to the documentation

根据文件

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

new RegExp(pattern[, flags])

pattern

模式

The text of the regular expression.

正则表达式的文本。

flags If specified, flags can have any combination of the following values:

flags如果指定,flags可以包含以下值的任意组合:

g - global match

g - 全球比赛

i - ignore case

我 - 忽略案例

m - multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)

m - 多线;将开始和结束字符(^和$)视为处理多行(即匹配每行的开头或结尾(由\ n或\ r分隔),而不仅仅是整个输入字符串的开头或结尾)

y - sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes).

y - 粘;仅匹配目标字符串中此正则表达式的lastIndex属性指示的索引(并且不会尝试与任何后续索引匹配)。

#1


1  

Solution

You have passed t as the first parameter. It should not be the search value, but the pattern

您已将t作为第一个参数传递。它不应该是搜索值,而是模式

According to the documentation

根据文件

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/RegExp

new RegExp(pattern[, flags])

pattern

模式

The text of the regular expression.

正则表达式的文本。

flags If specified, flags can have any combination of the following values:

flags如果指定,flags可以包含以下值的任意组合:

g - global match

g - 全球比赛

i - ignore case

我 - 忽略案例

m - multiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)

m - 多线;将开始和结束字符(^和$)视为处理多行(即匹配每行的开头或结尾(由\ n或\ r分隔),而不仅仅是整个输入字符串的开头或结尾)

y - sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes).

y - 粘;仅匹配目标字符串中此正则表达式的lastIndex属性指示的索引(并且不会尝试与任何后续索引匹配)。