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

Extract a specific word from string in Javascript - Stack Overflow

programmeradmin3浏览0评论
#anotherdata=value#iamlookingforthis=226885#id=101&start=1

Given the string above how could I extract "iamlookingforthis=226885" in the string? value of it might change as this is dynamic. So, other instance might be "iamlookingforthis=1105". The location/sequence might also change, it could be in the middle or last part.

Thank you in advance.

#anotherdata=value#iamlookingforthis=226885#id=101&start=1

Given the string above how could I extract "iamlookingforthis=226885" in the string? value of it might change as this is dynamic. So, other instance might be "iamlookingforthis=1105". The location/sequence might also change, it could be in the middle or last part.

Thank you in advance.

Share Improve this question edited Jan 6, 2017 at 22:19 Adam Azad 11.3k5 gold badges30 silver badges72 bronze badges asked Jan 6, 2017 at 22:16 JkizmoJkizmo 991 gold badge1 silver badge2 bronze badges 4
  • 2 You told us what about the string might change, but in order to grab this specific part, we'll need to know what's constant. Is it always between #s? Is it always iamlookingforthis? – Tyler Roper Commented Jan 6, 2017 at 22:18
  • 1 Possible duplicate of How to check if one string contains another substring in JavaScript? – OneNeptune Commented Jan 6, 2017 at 22:18
  • Why iamlookingforthis=226885 instead of e.g. anotherdata=value or id=101&start=1? – Oriol Commented Jan 6, 2017 at 22:18
  • 1 Why has this question got so many downvotes? I understood what OP was asking immediately. @OneNeptune the question clearly asks how to "extract". What has OP's question got to do with checking if a string contains a value? – BugHunterUK Commented Jan 11, 2017 at 9:21
Add a ment  | 

2 Answers 2

Reset to default 8

You can use Regex to match a specific text.

Like this for example

var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var value = str.match(/#iamlookingforthis=(\d+)/i)[1];

alert(value); // 226885

Explanation from Regex101:

#iamlookingforthis= matches the characters #iamlookingforthis= literally (case insensitive)

  • 1st Capturing Group (\d+)
    • \d+ matches a digit (equal to [0-9])
    • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
  • Global pattern flags
    • i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

See

  • RegExp on MDN
  • Regex 101 - try regex and see the explanation of it and results

Another alternative would be to split the string. You could split it by #|?|&.

var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';
var parts = str.split(/[#\?&]/g); // split the string with these characters

// find the piece with the key `iamlookingforthis`
var filteredParts = parts.filter(function (part) {
  return part.split('=')[0] === 'iamlookingforthis';
});

// from the filtered array, get the first [0]
// split the value and key, and grab the value [1]
var iamlookingforthis = filteredParts[0].split('=')[1];

alert(iamlookingforthis); // 226885

Here's a snippet:

var str = '#anotherdata=value#iamlookingforthis=226885#id=101&start=1';

var extracted = str.split("#").find(function(v){ 
  return v.indexOf("iamlookingforthis") > -1;
});

alert(extracted); // iamlookingforthis=226885

发布评论

评论列表(0)

  1. 暂无评论