我将从AJAX调用中接收以下字符串格式: [link = {https://www.w3schools.com text = here]
我需要提取“ link =”之后的值,然后提取“ text =”之后的值,因此,理想的输出是将“ https://www.w3schools.com”分配给变量,然后将“ here”分配给变量,如图所示。下面的代码。 “ link =”和“ text =”的值将更改。
我尝试使用正则表达式匹配并在Javascript中使用.split,但是我无法正确获得预期的值。
var str = "[link=https://www.w3schools.com text=here]";
var link = str.match(/link=(.*)/)[1]; //gets the link but includes rest of string
var linkText = str.match(/text=(.*)/)[1]; //gets "here" plus the closing bracket
答案 0 :(得分:0)
在您的图案中添加“ \ s” 和“]” ,试试这个
var str = "[link=https://www.w3schools.com text=here]";
var link = str.match(/link=(.*)\s/)[1]; //gets the link but includes rest of string
var linkText = str.match(/text=(.*)]/)[1];
console.log(link);
console.log(linkText);
答案 1 :(得分:0)
您可以使用
IFERROR
VLOOKUP
IF
YEAR
正则表达式为
var str = "[link=https://www.w3schools.com text=here]";
var m, res=[], rx=/(?:^|[\s[])(link|text)=([^\][\s]*)/g;
while (m = rx.exec(str)) {
console.log(m[1], "=", m[2]);
}
请参见regex demo。
详细信息
/(?:^|[\s[])(link|text)=([^\][\s]*)/
-字符串或空白或(?:^|[\s[])
[
-第1组:链接或文字(link|text)
-一个=
符号=
-组2:除([^\][\s]*)
,[
和空格之外的任何0+个字符。