0

I fount this link regarding my post How to extract a substring using regex but it is in java.

let's say i have a sentence:

var sentence = "This is a long sentence but I only want this 'information'";

Then i want to extract the value in the quotation marks (the value: information) and store it in a variable. What do I do?

Community
  • 1
  • 1
George V
  • 59
  • 5
  • What have you tried doing? Do you know that JavaScript has Regex [as well](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions)? – UnholySheep Jan 07 '17 at 17:46

2 Answers2

1

You can use String.prototype.slice(), String.prototype.indexOf(), String.prototype.lastIndexOf()

var sentence = "This is a long sentence but I only want this 'information'";

var res = sentence.slice(sentence.indexOf("'") + 1, sentence.lastIndexOf("'"));

console.log(res);
guest271314
  • 1
  • 15
  • 104
  • 177
1

It is very similar to the Java-based one in the link you gave:

var sentence = "This is a long sentence but I only want this 'information'";
var re = new RegExp("'(.*?)'");
console.log(sentence.match(re));
volatilevar
  • 1,626
  • 14
  • 16