用javascript解析输出
我想检查一个字符串,如果它没有用javascript解析输出,javascript,regex,string,Javascript,Regex,String,我想检查一个字符串,如果它没有开始,那么我不想做任何事情 例如mysqtinr=23435 acs 正如您所看到的,没有启动 但是如果字符串有以下内容,那么我想把它解析出来 myString=这里有一些文本,还有一些其他东西:只是更多的测试http://website/index.php 第43行1234 acd 我想解析出最后一个 我该怎么做 感谢var index=myString.lastIndexOf(“”); 如果(索引>0) myString=myString.substring(
开始,那么我不想做任何事情例如
mysqtinr=23435 acs
正如您所看到的,没有
启动
但是如果字符串有以下内容,那么我想把它解析出来
myString=
这里有一些文本,还有一些其他东西:只是更多的测试http://website/index.php 第43行
1234 acd
我想解析出最后一个
我该怎么做
感谢var index=myString.lastIndexOf(“
”);
如果(索引>0)
myString=myString.substring(index+“
”.length);
var myregexp=/((?:(?!))*)$/;
var match=myregexp.exec(主题);
如果(匹配!=null){
结果=匹配[1];
}否则{
结果=”;
}
说明:
<br\s*/> # match <br /> with optional space
( # capture the following:
(?: # repeat the following, but don't capture (because the outer parens do so already)
(?!<br\s*/>) # assert that it's impossible to match <br />
. # if so, match any character
)* # do this as many times as necessary...
) # (end of capturing group)
$ # ...until the end of the string.
#用可选空格匹配
(#捕获以下内容:
(?:#重复以下步骤,但不要捕捉(因为外部参数已经捕捉到了)
(?!)#断言不可能匹配
.#如果是,请匹配任何字符
)*#根据需要多次这样做。。。
)#(捕获组结束)
$#…直到字符串结束。
因此,我们首先尝试匹配
或
等。如果匹配失败,则匹配失败。如果没有,那么我们将捕获下面的每个字符,直到字符串的末尾,除非在此过程中可以匹配另一个
。这确保了我们确实从最后一次
开始进行匹配。结果将出现在backreference nr.1中(如果在最后一次
之后没有任何内容,则该结果可能为空)。我曾经使用JavaScript发布过一次:p由于标记
和
有效,因此此项不起作用。明白你说的“有效”是什么意思吗?他想要最后一个br
tag后面的字符串。我的意思是,如果你试图解析一个有
的字符串,你的方法将找不到它。问题是关于
的。但该示例可以轻松扩展以支持其他标记/标记表示,如
、
或
。该示例周围也没有单引号或双引号。我的观点是,直接用javascript函数substr
和indexOf
解析HTML只会让他将来头疼。
var myregexp = /<br\s*\/>((?:(?!<br\s*\/>).)*)$/;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1];
} else {
result = "";
}
<br\s*/> # match <br /> with optional space
( # capture the following:
(?: # repeat the following, but don't capture (because the outer parens do so already)
(?!<br\s*/>) # assert that it's impossible to match <br />
. # if so, match any character
)* # do this as many times as necessary...
) # (end of capturing group)
$ # ...until the end of the string.