最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using jQuery to find a substring - Stack Overflow

programmeradmin5浏览0评论

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!

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!

Share Improve this question edited Dec 30, 2011 at 10:40 Dayo 12.8k5 gold badges55 silver badges68 bronze badges asked Jun 12, 2010 at 3:40 SteveSteve 591 gold badge1 silver badge2 bronze badges 4
  • First of all, you don't use jQuery to do string manipulation - that's Javascript/ECMAScript. Secondly, can you be more specific about the logic used to grab that string? Is there a pattern? – meder omuraliev Commented Jun 12, 2010 at 3:44
  • Question for jQuery not tagged with jQuery – sushil bharwani Commented Jun 12, 2010 at 3:45
  • Well anyways why need jQuery? cannot simple javascript work for you – sushil bharwani Commented Jun 12, 2010 at 3:47
  • In my eyes, this question shouldn't be tagged with jQuery for the very reason that it has nothing to do with jQuery. ;) – Gert Grenander Commented Jun 12, 2010 at 3:57
Add a comment  | 

4 Answers 4

Reset to default 6

This has nothing to do with jQuery, which is primarily for DOM traversal and manipulation. 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.

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. The rest breaks down as follows:

  • ^ start at the beginning of the string
  • .* a series of 0 or more characters
  • ABC your opening delimiter
  • (.*) match a series of 0 or more characters
  • XYZ your closing delimiter
  • .* a series of 0 or more characters
  • $ match to the end of the string

The second parameter, the replacement string, is '$1'. replace will 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.

You may not need to use jQuery on this one. 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. In my experience these types of functions work the same across all browsers.

Meagar, your explanation is great, and clearly explains who it works.

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:

^
.*
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).

Thanks,

Steve

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 P tag:

<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):

$(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. I hope this clears the confusion.

发布评论

评论列表(0)

  1. 暂无评论