0

How to loop through a string and add its value inside of regex in the match method. I'm getting null. Tried with /'str[i]'/g and it also gives null.

var str = "helloWorld";
var regResult;

for (var i = 0; i < str.length; i++) {
  regResult = str.match(/str[i]/g); //gives null
};
cнŝdk
  • 28,676
  • 7
  • 47
  • 67
Deke
  • 3,837
  • 2
  • 35
  • 53
  • 1
    [javascript - How do you use a variable in a regular expression?](https://stackoverflow.com/questions/494035/how-do-you-use-a-variable-in-a-regular-expression) – Andreas Sep 17 '18 at 14:12

4 Answers4

1

Right now your regular expression is matching str followed by a single character from the range i - one single i, meaning it will match stri. To match the variable i, try the following:

var str = "helloWorld";
var regResult;

for (var i = 0; i < str.length; i++) {
  regResult = str.match(new RegExp('str' + i, 'g')); //gives null
};

Here we are creating a regular expression whose pattern contains the current value of the variable i. As an example, if i is 4 then the regular expression will be constructed as if you had simply given /str4/g to str.match.

EDIT

To reflect the edit made to the question, my new proposed solution is as follows:

var str = "helloWorld";
var regResult;

for (var i = 0; i < str.length; i++) {
  regResult = str.match(new RegExp(str[i], 'g')); //gives null
};

This code differs from the above code in that it is reading the value i from str. For example if i is 4 and str[4] = "h", then the regular expression will be constructed as if you had simply given the value of str[4] to str.match: str.match(/h/g).

Community
  • 1
  • 1
Jamie Ridding
  • 375
  • 2
  • 11
  • I've updated my answer to reflect the change to the question @Deke – Jamie Ridding Sep 17 '18 at 14:25
  • But this will insert only `['d']` in the `regResult` array, from the last iteration, it needs to be adjusted. and also what's `ars`? – cнŝdk Sep 17 '18 at 14:59
  • I'm not actually sure where `ars` came from @cнŝdk. I was probably typing when I was tired (again). It's been fixed now – Jamie Ridding Jan 31 '19 at 09:20
  • 1
    Wait, nevermind. `ars` was the original name of the variable in the question, before it was edited. I must have missed the name change. – Jamie Ridding Jan 31 '19 at 09:27
1

Probably you need just a little bit fix

var str = "helloWorld";
var regResult;

for (var i = 0; i < str.length; i++) {
    regResult = str.match(new RegExp(str[i], 'g'));
    console.log(regResult)
};
harry
  • 549
  • 3
  • 13
1
// static RegExp -> /str[i]/g is just equal "str[i]"
// dynamic RegExp -> new RegExp(str[i], "g") is str[i] h,e,l,l, ...

var str = "helloWorld";
var regResult;

for (var i = 0; i < str.length; i++) {
  regResult = str.match(new RegExp(str[i], "g")); //gives null
  console.log(regResult);
};
seunggabi
  • 1,302
  • 7
  • 11
1

You need to use the new RegExp() constructor, so you can dynamically create the regex with the iterated string:

str.match(new RegExp(str[i],'g'))

But if you are trying to get an array of matches for every letter in the string you need to push the matches array in your regResult and not just keep overrding its value on every iteration:

regResult.push(str.match(new RegExp(str[i], 'g'))); //gives null

Demo:

var str = "helloWorld";
var regResult = [];

for (var i = 0; i < str.length; i++) {
  regResult.push(str.match(new RegExp(str[i], 'g'))); //gives null
};
console.log(regResult);
cнŝdk
  • 28,676
  • 7
  • 47
  • 67