如何在javascript regex中匹配字符之间的字符串

时间:2023-01-14 14:14:52

I'm trying to match just the characters between some set characters using regex? I'm very new to this but I'm getting somewhere...

我在用正则表达式匹配一些字符集字符之间的字符?我对这个很陌生,但我正在取得一些进展……

I want to match all instances of text between '[[' and ']]' in the following string:

我希望在以下字符串中匹配“[[]和]”之间的所有文本实例:

'Hello, my [[name]] is [[Joffrey]]'.

“你好,我的[名字]是[乔佛里]]。”

So far I've been able to retrieve [[name and [[Joffrey with the following regex:

到目前为止,我已经能够检索[[name and [Joffrey]与以下regex:

\[\[([^\]])*\g

I've experimented with grouping etc but can't seem to get the 'contents' only (name and Joffrey).

我尝试过分组等等,但似乎只能得到“内容”(name和Joffrey)。

Any ideas?

什么好主意吗?

Thanks

谢谢

4 个解决方案

#1


1  

var regex = /\[\[(.*?)\]\]/g;
var input = 'Hello, my my [[name]] is [[Joffrey]]';
var match;

do {
    match = regex.exec(input);
    if (match) {
        console.log(match[1]);
    }
} while (match);

Will print both matches in your console. Depending on whether you want to print out even blank values you would want to replace the "*" with a "+" /\[\[(.+?)\]\]/g.

将在您的控制台中打印两个匹配项。根据您是否想打印甚至是空白的值,您希望用“+”/\[\ (.+?)\]/g替换“*”。

#2


1  

Here is the regex:

这是正则表达式:

/\[\[(.*?)\]]/g

Explanation:

解释:

\[ Escaped character. Matches a "[" character (char code 91).

( Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.

. Dot. Matches any character except line breaks.
* Star. Match 0 or more of the preceding token.
? Lazy. Makes the preceding quantifier lazy, causing it to match as few characters as possible.
)
\] Escaped character. Matches a "]" character (char code 93).
] Character. Matches a "]" character (char code 93).

#3


0  

try this /\[\[(\w+)\]\]/g

试试这个/ \[\[(\ w +)\]\]/ g

Demo in regex101 https://regex101.com/r/xX1pP0/1

演示在regex101 https://regex101.com/r/xX1pP0/1

#4


0  

var str = 'Hello, my [[name]] is [[Joffrey]]';
var a = str.match(/\[\[(.*?)\]\]/g);

#1


1  

var regex = /\[\[(.*?)\]\]/g;
var input = 'Hello, my my [[name]] is [[Joffrey]]';
var match;

do {
    match = regex.exec(input);
    if (match) {
        console.log(match[1]);
    }
} while (match);

Will print both matches in your console. Depending on whether you want to print out even blank values you would want to replace the "*" with a "+" /\[\[(.+?)\]\]/g.

将在您的控制台中打印两个匹配项。根据您是否想打印甚至是空白的值,您希望用“+”/\[\ (.+?)\]/g替换“*”。

#2


1  

Here is the regex:

这是正则表达式:

/\[\[(.*?)\]]/g

Explanation:

解释:

\[ Escaped character. Matches a "[" character (char code 91).

( Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.

. Dot. Matches any character except line breaks.
* Star. Match 0 or more of the preceding token.
? Lazy. Makes the preceding quantifier lazy, causing it to match as few characters as possible.
)
\] Escaped character. Matches a "]" character (char code 93).
] Character. Matches a "]" character (char code 93).

#3


0  

try this /\[\[(\w+)\]\]/g

试试这个/ \[\[(\ w +)\]\]/ g

Demo in regex101 https://regex101.com/r/xX1pP0/1

演示在regex101 https://regex101.com/r/xX1pP0/1

#4


0  

var str = 'Hello, my [[name]] is [[Joffrey]]';
var a = str.match(/\[\[(.*?)\]\]/g);