0

I'm attempting to extract strings between occurences of a specific character in a larger string.

For example:

The initial string is:

var str = "http://www.google.com?hello?kitty?test";

I want to be able to store all of the substrings between the question marks as their own variables, such as "hello", "kitty" and "test".

How would I target substrings between different indexes of a specific character using either JavaScript or Regular Expressions?

JWill
  • 1
  • 3

6 Answers6

0

Quick note: that URL would be invalid. A question mark ? denotes the beginning of a query string and key/value pairs are generally provided in the form key=value and delimited with an ampersand &.

That being said, if this isn't a problem then why not split on the question mark to obtain an array of values?

var split_values = str.split('?');
//result: [ 'http://www.google.com', 'hello', 'kitty', 'test' ]

Then you could simply grab the individual values from the array, skipping the first element.

B. Fleming
  • 5,778
  • 1
  • 16
  • 29
  • Thanks for the solution! It works great and does the job, I went with the other answer as it doesn't store the URL as the first index. – JWill Jun 11 '18 at 17:15
0

I believe this will do it:

var components = "http://www.google.com?hello?kitty?test".split("?");
components.slice(1-components.length) // Returns: [ "hello", "kitty", "test" ]
0

using Regular Expressions

var reg = /\?([^\?]+)/g;
var s = "http://www.google.com?hello?kitty?test";

var results = null;

while( results = reg.exec(s) ){
  console.log(results[1]);
}
Stakvino
  • 542
  • 3
  • 8
  • Thanks for the regex solution! This does exactly what I was looking for, a bit more out of my realm of understanding compared to a JS only solution though. – JWill Jun 11 '18 at 17:13
0

The general case is to use RegExp:

var regex1 = new RegExp(/\?.*?(?=\?|$)/,'g'); regex1.lastIndex=0;
str.match(regex1)

Note that this will also get you the leading ? in each clause (no look-behind regexp in Javascript).

Alternatively you can use the sticky flag and run it in a loop:

var regex1 = new RegExp(/.*?\?(.*?)(?=\?|$)/,'y'); regex1.lastIndex=0;
while(str.match(regex1)) {...}
Gabi Lee
  • 845
  • 5
  • 12
0

You can take the substring starting from the first question mark, then split by question mark

const str = "http://www.google.com?hello?kitty?test";
const matches = str.substring(str.indexOf('?') + 1).split(/\?/g);
console.log(matches);
baao
  • 62,535
  • 14
  • 113
  • 168
  • Thanks for this. The solution works great, however it stores the URL in the array if no ?'s and additional terms are added to the end of the URL. – JWill Jun 11 '18 at 17:10
0

You could split on ? and use slice passing 1 as the parameter value.

That would give you an array with your values. If you want to create separate variables you could for example get the value by its index var1 = parts[0]

var str = "http://www.google.com?hello?kitty?test";
var parts = str.split('?').slice(1);
console.log(parts);
var var1 = parts[0],
  var2 = parts[1],
  var3 = parts[2];
console.log(var1);
console.log(var2);
console.log(var3);
The fourth bird
  • 96,715
  • 14
  • 35
  • 52
  • Thanks! This solution worked best for me as it was the most straight forward and easy to understand code snippet. I ended up simply calling the different index's inside the array rather than storing them in separate variables. I also like how it doesn't store the URL as the first index and if there aren't any ?'s in the string, it wont index anything. – JWill Jun 11 '18 at 17:07