Javascript 使用 jQuery 查找子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3027379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jQuery to find a substring
提问by Steve
Say you have a string: "The ABC cow jumped over XYZ the moon" and you want to use jQuery to get the substring between the "ABC" and "XYZ", how would you do this? The substring should be "cow jumped over". Many thanks!
假设您有一个字符串:“ABC 牛跳过月球 XYZ”,并且您想使用 jQuery 获取“ABC”和“XYZ”之间的子字符串,您将如何执行此操作?子串应该是“cow jumped over”。非常感谢!
回答by meagar
This has nothing to do with jQuery, which is primarily for DOM traversal and manipulation. You want a simple regular expression:
这与 jQuery 无关,它主要用于 DOM 遍历和操作。你想要一个简单的正则表达式:
var str = "The ABC cow jumped over XYZ the moon";
var sub = str.replace(/^.*ABC(.*)XYZ.*$/m, '');
The idea is you're using a String.replacewith a regular expression which matches your opening and closing delimiters, and replacing the whole string with the part matched between the delimiters.
这个想法是你使用一个String.replace和一个匹配你的开始和结束分隔符的正则表达式,并用分隔符之间匹配的部分替换整个字符串。
The first argument is a regular expression. The trailing mcauses it to match over multiple lines, meaning your text between ABCand XYZmay contain newlines. The rest breaks down as follows:
第一个参数是一个正则表达式。尾随m使其匹配多行,这意味着你之间的文本ABC,并XYZ可能包含换行符。其余的分解如下:
^start at the beginning of the string.*a series of 0 or more charactersABCyour opening delimiter(.*)match a series of 0 or more charactersXYZyour closing delimiter.*a series of 0 or more characters$match to the end of the string
^从字符串的开头开始.*一系列 0 个或多个字符ABC你的开始分隔符(.*)匹配一系列 0 个或多个字符XYZ你的结束分隔符.*一系列 0 个或多个字符$匹配到字符串的末尾
The second parameter, the replacement string, is '$1'. replacewill substitute in parenthesized submatchs from your regular exprsesion - the (.*)portion from above. Thus the return value is the entire string replace with the parts between the delimiters.
第二个参数,替换字符串,是'$1'。 replace将替换来自您的常规表达式的括号子匹配 -(.*)上面的部分。因此,返回值是整个字符串替换为分隔符之间的部分。
回答by dana
You may not need to use jQuery on this one. I'd do something like this:
你可能不需要在这个上使用 jQuery。我会做这样的事情:
function between(str, left, right) {
if( !str || !left || !right ) return null;
var left_loc = str.indexOf(left);
var right_loc = str.indexOf(right);
if( left_loc == -1 || right_loc == -1 ) return null;
return str.substring(left_loc + left.length, right_loc);
}
No guarantees the above code is bug-free, but the idea is to use the standard substring()function. In my experience these types of functions work the same across all browsers.
不能保证上面的代码没有错误,但我们的想法是使用标准的substring()函数。根据我的经验,这些类型的函数在所有浏览器中的工作方式相同。
回答by Steve
Meagar, your explanation is great, and clearly explains who it works.
Meagar,您的解释很棒,并且清楚地解释了它的作用。
Just a few minor questions:
只是几个小问题:
Are the () parenthesis required ONLY as a way to indicate a submatch in the second parameter of the relpace function or would this also identify the submatches: /^.*ABC.XYZ.$/ but not work for what we are trying to do in this case?
Does this regular expression have 7 submatches:
() 括号是否仅需要作为指示 relpace 函数的第二个参数中的子匹配的一种方式,或者这也可以标识子匹配:/^.*ABC。XYZ。$/ 但不适用于我们在这种情况下尝试做的事情?
此正则表达式是否有 7 个子匹配项:
^
.*
ABC
.*
XYZ
.*
$
^
.*
ABC
.*
XYZ
.*
$
- Does the $1 mean to use the first parenthesized submatch? At first I thought it might mean to use the second submatch in the series (the first being $0).
- $1 是否意味着使用第一个带括号的子匹配?起初我认为这可能意味着使用系列中的第二个子匹配(第一个是 $0)。
Thanks,
谢谢,
Steve
史蒂夫
回答by Gert Grenander
Just to show you how you would use jQuery and meagar's regex. Let's say that you've got an HTML page with the following Ptag:
只是为了向您展示如何使用 jQuery 和 meagar 的正则表达式。假设您有一个带有以下P标记的 HTML 页面:
<p id="grabthis">The ABC cow jumped over XYZ the moon</p>
To grab the string, you would use the following jQuery/JavaScript mix (sounds kind of stupid, since jQuery is JavaScript, but see jQuery as a JavaScript DOM library):
要获取字符串,您将使用以下 jQuery/JavaScript 组合(听起来有点愚蠢,因为 jQuery 是 JavaScript,但将 jQuery 视为 JavaScript DOM 库):
$(document).ready(function() { // Wait until the document has been fully loaded
var pStr=$("#grabthis").text(); // Grab the text from the P tag and put it into a JS variable
var subStr=pStr.replace(/^.*ABC(.*)XYZ.*$/m, ''); // Run the regex to grab the middle string
alert(subStr); // Output the grabbed middle string
});
Or the shorter version:
或者更短的版本:
$(document).ready(function() {
alert($("#grabthis").text().replace(/^.*ABC(.*)XYZ.*$/m, ''));
});
The replace function is a JavaScript function. I hope this clears the confusion.
替换函数是一个 JavaScript 函数。我希望这能消除混乱。

