0

I have a text

"Follow Up=10;Meeting=90;Research=20;Transferred=83;"

It's not necessary that the text starts with Follow Up

I want to extract only the value associated to Follow Up which is 10. Can you please provide jQuery code to get that?

3
  • You can use string in JSON format and then parse to get specific value Commented Nov 23, 2015 at 9:03
  • 1
    or use a simple regex /Follow Up=[0-9]+/gi Commented Nov 23, 2015 at 9:05
  • Hi, thanks for your replies. Can you provide the code using the Regex? Commented Nov 23, 2015 at 9:07

2 Answers 2

2

You don't need jQuery to do this, you can use plain Javascript:

var string = 'Follow Up=10;Meeting=90;Research=20;Transferred=83;';

//remove trailing semicolon
string = string.replace(/;$/, '');

var values = {};

string.split(';').forEach(function(item) {
  var components = item.split('=');
  values[components[0]] = components[1];
});
Sign up to request clarification or add additional context in comments.

Comments

-1

You can do it using split:

var keyValues = [];
var string = 'Follow Up=10;Meeting=90;Research=20;Transferred=83;';

string.split(';').forEach(function(elemString){
   var elem = elemString.split('=');
   if(elem.length == 2){
      keyValues[elem[0]]=elem[1];
   }
});

console.log(keyValues['Follow Up']); //you can acces any of the other keys as well like keyValues['Meeting'].

Edit: mixed with Brendan Nee's answer so no jQuery is needed.

With the regex:

var Regex =  /Follow Up=([0-9]+)/gi;
var matches = Regex.exec(string);
console.log(matches[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.