简体   繁体   English

使用jQuery查找子字符串

[英]Using jQuery to find a substring

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? 假设你有一个字符串:“ABC牛跳过XYZ月亮”你想用jQuery来获取“ABC”和“XYZ”之间的子串,你会怎么做? The substring should be "cow jumped over". 子串应该是“牛跳过”。 Many thanks! 非常感谢!

This has nothing to do with jQuery, which is primarily for DOM traversal and manipulation. 这与jQuery无关,jQuery主要用于DOM遍历和操作。 You want a simple regular expression: 你想要一个简单的正则表达式:

var str = "The ABC cow jumped over XYZ the moon";
var sub = str.replace(/^.*ABC(.*)XYZ.*$/m, '$1');

The idea is you're using a String.replace with 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 m causes it to match over multiple lines, meaning your text between ABC and XYZ may contain newlines. 尾随m使其匹配多行,这意味着ABCXYZ之间的文本可能包含换行符。 The rest breaks down as follows: 其余细分如下:

  • ^ start at the beginning of the string ^从字符串的开头开始
  • .* a series of 0 or more characters .*一系列0个或更多字符
  • ABC your opening delimiter ABC你的开场分隔符
  • (.*) match a series of 0 or more characters (.*)匹配一系列0个或多个字符
  • XYZ your closing delimiter XYZ你的结算分隔符
  • .* a series of 0 or more characters .*一系列0个或更多字符
  • $ match to the end of the string $匹配字符串的结尾

The second parameter, the replacement string, is '$1'. 第二个参数,即替换字符串,是'$ 1'。 replace will substitute in parenthesized submatchs from your regular exprsesion - the (.*) portion from above. replace将替换来自常规exprseion的括号中的子匹配 - 上面的(.*)部分。 Thus the return value is the entire string replace with the parts between the delimiters. 因此,返回值是整个字符串替换为分隔符之间的部分。

You may not need to use jQuery on this one. 你可能不需要在这个上使用jQuery。 I'd do something like this: 我会做这样的事情:

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. 不保证上面的代码没有错误,但想法是使用标准的substring()函数。 In my experience these types of functions work the same across all browsers. 根据我的经验,这些类型的功能在所有浏览器中都是相同的。

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. 是否仅需要()括号作为在relpace函数的第二个参数中指示子匹配的方法,或者这也将标识子匹配:/^.*ABC。 XYZ. XYZ。 $/ but not work for what we are trying to do in this case? $ /但不适用于我们在这种情况下要做的事情?

  • Does this regular expression have 7 submatches: 这个正则表达式有7个子匹配:

^ ^
.* *。
ABC ABC
.* *。
XYZ XYZ
.* *。
$ $

  • Does the $1 mean to use the first parenthesized submatch? $ 1是否意味着使用第一个带括号的子匹配? At first I thought it might mean to use the second submatch in the series (the first being $0). 起初我认为这可能意味着在系列中使用第二个子匹配(第一个是$ 0)。

Thanks, 谢谢,

Steve 史蒂夫

Just to show you how you would use jQuery and meagar's regex. 只是为了向您展示如何使用jQuery和meagar的正则表达式。 Let's say that you've got an HTML page with the following P tag: 假设您有一个带有以下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, '$1'); // 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, '$1'));
});

The replace function is a JavaScript function. replace函数是一个JavaScript函数。 I hope this clears the confusion. 我希望这能解决这个困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM