How to get all text following the symbol ":
"?
I have tried:
'prop:bool*'.match(/:(.[a-z0-9]+)/ig)
But it returns [":bool"]
not ["bool"]
.
Update:
I need to use this inside the following expression:
'prop:bool*'.match(/^[a-z0-9]+|:.[a-z0-9]+|\*/ig);
So that the result becomes:
["prop", "bool", "*"]
答案 0 :(得分:2)
You could solve this by performing a positive lookbehind action.
'prop:bool*'.match(/^[a-z0-9]+|(?<=:).[a-z0-9]+|\*/ig)
The positive lookbehind is the (?<=:)
part of the regex and will here state a rule of must follow ':'
.
The result should here be ["prop", "bool", "*"]
.
Edit:
Original requirements were somewhat modified by original poster to return three groups of answers. My original code, returning one answer, was the following:
'prop:bool*'.match(/(?<=:).[a-z0-9]+/ig)
答案 1 :(得分:1)
这不是纯正则表达式解决方案,因为它利用了String对象及其substring()方法的优势,如下所示:
/// <reference types="@types/googlemaps" />
匹配成功后,一个元素组成的数组将保留值var str = 'prop:bool*';
var match = str.match(/:(.[a-z0-9]+)/ig).pop().substring(1,str.length);
console.log(match);
。该结果仅需要提取:bool
部分。因此,该元素使用其pop()方法返回字符串值。字符串依次使用其substring()方法绕过':'并提取所需的部分,即bool
。
bool
要返回三组数据,代码使用捕获组并通过使用var [a,b,c] = 'prop:bool*'.match(/^([a-z0-9]+)|:(.[a-z0-9]+)|(\*)/ig);
console.log(a,b.substring(1,b.length),c);
的substring()方法来修剪冒号。
答案 2 :(得分:0)
You could simply do:
'prop:bool*'.match(/:(.[a-z0-9]+)/)[1]
答案 3 :(得分:0)
If your entire string is of the form you show, you could just use a regex with capture groups to get each piece:
console.log('prop:bool*'.match(/^([a-z0-9]+):(.[a-z0-9]+)(\*)/i).slice(1));