[英]regex search a string for contents between two strings
I am trying my upmost best to get my head around regex, however not having too much luck. 我正在尽我最大的努力使我正视正则表达式,但是运气不高。
I am trying to search within a string for text, I know how the string starts, and i know how the string ends, I want to return ALL the text inbetween the string including the start and end. 我正在尝试在字符串中搜索文本,我知道字符串是如何开始的,并且我知道字符串是如何结束的,我想返回字符串之间的所有文本,包括开始和结束。
Start search = [{"lx": 开始搜索= [{“ lx”:
End search = }] 结束搜索= }]
ie 即
[{"lx": variablehere }] [{“ lx”:此处为变量 }]
So far I have tried 到目前为止,我已经尝试过
/^\[\{"lx":(*?)\}\]/;
and 和
/(\[\{"lx":)(*)(\}\])/;
But to no real avail... can anyone assist? 但是无济于事...谁能协助?
Many thanks 非常感谢
The *
star character multiplies the preceding character. *
字符乘以前面的字符。 In your case there's no such character. 在您的情况下,没有这样的角色。 You should either put
.
你要么放
.
, which means "any character", or something more specific like \\S
, which means "any non whitespace character". ,表示“任何字符”,或更具体的名称,如
\\S
,表示“任何非空白字符”。
You're probably making the mistake of believing the *
is a wildcard. 您可能会误以为
*
是通配符。 Use the period ( .
) instead and you'll be fine. 使用句号(
.
),就可以了。
Also, are you sure you want to stipulate zero or more? 另外,您确定要指定零个或多个吗? If there must be a value, use + (one or more).
如果必须有一个值,请使用+(一个或多个)。
Javascript: 使用Javascript:
'[{"lx":variablehere}]'.match(/^\[\{"lx":(.+?)\}\]/);
Possible solution: 可能的解决方案:
var s = '[{"lx":variablehere}]';
var r = /\[\{"(.*?)":(.*?)\}\]/;
var m = s.match(r);
console.log(m);
Results to this array: 结果到这个数组:
[ '[{"lx":variablehere}]',
'lx',
'variablehere',
index: 0,
input: '[{"lx":variablehere}]' ]
\[\{"lx"\:(.*)\}\]
This should work for you. 这应该为您工作。 You can reach the captured variable by \\1 notation.
您可以通过\\ 1表示法来获取捕获的变量。
Try this: 尝试这个:
^\[\{\"lx\"\:(.*)\}\]$
all text between [{"lx": and }] you will find in backreference variable (something like \\$1 , depends on programming language). 在[{“ lx”:和}]之间的所有文本都可以在反向引用变量中找到(类似于\\ $ 1,取决于编程语言)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.