javascript正则表达式问号后面的括号

时间:2021-01-25 22:25:40

I want to capture some string only if it's appear so i use ? after capture () meaning i want to capture that part of that string but it does not have to be shown (zero or one) but when i add ? after () it drop that match:

我想要捕获一些字符串,只有当它出现时,我才使用?在capture()之后,意味着我想要捕获那个字符串的一部分,但它不需要显示(0或1),但是当我添加时?after()它去掉匹配项:

var str = 'blablablacaptureblablabla';

if i make a regular capture with () i get the desire capture:

如果我用()进行常规捕获,我就会得到期望捕获:

console.log(str.match(/.*(capture).*/i)); // array[1] = capture

if i add ? to indicate capture may be or not be at all i get undefined:

如果我补充的吗?为了表明捕获是否可能,我没有定义:

console.log(str.match(/.*(capture)?.*/i)); // array[1] = undefined

why is that ? all i want is the capture the word capture whether its present or not in the string so this will not return null:

这是为什么呢?我想要的是捕获字捕获无论它在字符串中是否存在所以这个不会返回null:

var str = 'blablablalablabla'; //string without word 'capture'
console.log(str.match(/.*(capture)?.*/i)); // this will work but if i use it with string with the word 'capture' it won't capture the 'capture'

EDIT:

编辑:

Just to be clear - I want this string blablacaptureblabla to capture the word capture and also this string blablablabla to not return null cause i use ? which means zero or one

明确一点,我想让这个字符串blablacaptureblabla捕捉单词捕获,还有这个字符串blablablablablablablablablabla不返回null因为我用了?是0还是1

2 个解决方案

#1


4  

If you want the capture value to be always initialized (to avoid undefined value for Capturing group #1) you need to use an obligatory group with an empty alternation and use the tempered greedy token (?:(?!capture).)*:

如果您希望捕获值总是初始化(为了避免捕获组#1的未定义值),您需要使用一个具有空交替的强制组,并使用调和的贪婪令牌(?:(?!capture))*:

/^(?:(?!capture).)*(capture|).*/i

See regex demo

查看演示正则表达式

A tempered greedy token is a special construct made of a (non-)capturing group ((?:...) or (...)) matching a single character that is not starting a specific sequence (that is specified with a negative lookahead, (?!...)), to which a quantifier is applied.

缓变贪心令牌是由(非)捕获组((?:…)或(…))组成的特殊结构,它匹配一个不启动特定序列的单个字符(用一个否定的前视符号(?!…)指定的字符),对其应用量词。

Below is a JS demo:

下面是一个JS演示:

var str = 'blablablalablabla'; //string without word 'capture'
document.body.innerHTML = '"' + str.match(/^(?:(?!capture).)*(capture)?.*/i)[1] + '"<br/>'; // undefined, as the group is optional
document.body.innerHTML += '"' + str.match(/^(?:(?!capture).)*(capture|).*/i)[1] + '"<br/>'; //  empty string, the group is obligatory
var str1 = 'blablabcapturelalablabla';
document.body.innerHTML += '"' + str1.match(/^(?:(?!capture).)*(capture|).*/i)[1] + '"';

#2


-1  

if you use the global modifier it will do what you want...

如果你使用全局修饰符,它会做你想做的…

var str = 'blablablalablabla'; //string without word 'capture'
console.log(str.match(/.*(capture)?.*/ig)); // array[1] = ""

#1


4  

If you want the capture value to be always initialized (to avoid undefined value for Capturing group #1) you need to use an obligatory group with an empty alternation and use the tempered greedy token (?:(?!capture).)*:

如果您希望捕获值总是初始化(为了避免捕获组#1的未定义值),您需要使用一个具有空交替的强制组,并使用调和的贪婪令牌(?:(?!capture))*:

/^(?:(?!capture).)*(capture|).*/i

See regex demo

查看演示正则表达式

A tempered greedy token is a special construct made of a (non-)capturing group ((?:...) or (...)) matching a single character that is not starting a specific sequence (that is specified with a negative lookahead, (?!...)), to which a quantifier is applied.

缓变贪心令牌是由(非)捕获组((?:…)或(…))组成的特殊结构,它匹配一个不启动特定序列的单个字符(用一个否定的前视符号(?!…)指定的字符),对其应用量词。

Below is a JS demo:

下面是一个JS演示:

var str = 'blablablalablabla'; //string without word 'capture'
document.body.innerHTML = '"' + str.match(/^(?:(?!capture).)*(capture)?.*/i)[1] + '"<br/>'; // undefined, as the group is optional
document.body.innerHTML += '"' + str.match(/^(?:(?!capture).)*(capture|).*/i)[1] + '"<br/>'; //  empty string, the group is obligatory
var str1 = 'blablabcapturelalablabla';
document.body.innerHTML += '"' + str1.match(/^(?:(?!capture).)*(capture|).*/i)[1] + '"';

#2


-1  

if you use the global modifier it will do what you want...

如果你使用全局修饰符,它会做你想做的…

var str = 'blablablalablabla'; //string without word 'capture'
console.log(str.match(/.*(capture)?.*/ig)); // array[1] = ""