Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 匹配两个字符之间的字符串regex_Javascript_Regex - Fatal编程技术网

Javascript 匹配两个字符之间的字符串regex

Javascript 匹配两个字符之间的字符串regex,javascript,regex,Javascript,Regex,我需要使用newregexp 如果两个字符之间有一个特定的字符串,我需要得到一个匹配,但是如果这些字符之间有一个类似的字符串,/和?则不需要匹配。 即: 要匹配的字符串为: ”https://www.mysite.se/should-match?ba=11“ 我有应该是ma 这应该不会给出任何匹配。但是应该匹配应该给出一个匹配 所以我需要创建newregexp() 有什么想法吗?试试这个: (?!\/)[^\/\?]*(?=\?) 将\/替换为开始分隔符,将\?替换为结束分隔符。如果开始或结

我需要使用
newregexp
如果两个字符之间有一个特定的字符串,我需要得到一个匹配,但是如果这些字符之间有一个类似的字符串,
/
则不需要匹配。 即:

要匹配的字符串为:

”https://www.mysite.se/should-match?ba=11“

我有
应该是ma

这应该
不会
给出任何匹配。但是
应该匹配
应该给出一个匹配 所以我需要创建
newregexp()

有什么想法吗?

试试这个:

(?!\/)[^\/\?]*(?=\?)

\/
替换为开始分隔符,将
\?
替换为结束分隔符。如果开始或结束分隔符中有任何一个
?*+^$[]\(){}124;-
,则需要在其前面添加一个
\
,或使用此函数为您执行以下操作:

var escape = function(str) {
    return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
};
备选方案:

var matcher = function(str, start, end){
    var quote = function(str) {
        return (str+'').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
    };
    return str.match(new RegExp(quote(start) + "[^" + quote(start) + quote(end) + "]*" + quote(end)))[0].slice(1, -1)
};

使用like
matcher(“https://www.mysite.se/should-match?ba=11“,”/“,“?”

这是用于PHP的,但您也应该能够在JavaScript中使用正则表达式。有多个站点也可以让您测试这一点@好的,谢谢。但是如何在
/
?\/(.*?)之间匹配特定文本这相当接近——我相信你可以从那里接受。这将为您提供“/www.mysite.se/should match”,因此您必须改进一点。谢谢,但我只需要在
新RegExp
中添加内容。请回答这个问题,我接受:)@user1665355已修复。问题仍然是我在mongo中使用它,并且我已经有了一个字符串,比如“应该ma”,我只需要有
新的Regexp
。也就是说,
MyField:new RegExp(…)
@user1665355是否对您无效?
(?!\/)[^\/\?]*(?=\?)
对您无效?它有效,但我
需要
说出我自己的话,而不是匹配
/
之间的内容。你明白吗?:)