0

My attempt was :

var re = new RegExp("\w{" + n + "}", "g");

But it didn't seems to work.

P.S. - I have searched several questions of Stackoverflow thinking it must have been asked before But I didn't find one, so I asked my question.

  • What is really what you want to do? – Mindastic Jul 26 '15 at 18:03
  • I want to split the string, into pairs according to the input (n) given by user. – Divyansh Batham Jul 26 '15 at 18:03
  • But what have you tried so far? Do you have more code? – Mindastic Jul 26 '15 at 18:04
  • For example String is 'Divyansh' and n is 2 => ['Di','vy','an','sh']; – Divyansh Batham Jul 26 '15 at 18:05
  • Just this in return statement `var arr = str.match(re);` where str is the variable having the string. – Divyansh Batham Jul 26 '15 at 18:06
  • actually this should work as is, you forgot that \ is also the escape character in JS, so it becomes \\, but you could just use `.` instead of `\\w` – maraca Jul 26 '15 at 18:07
  • What do you exactly meant 'maraca' ? I have used \w to represent alphanumeric characters. – Divyansh Batham Jul 26 '15 at 18:10
  • Yup, as @maraca says, you are missing a backslash. Maraca, why don't you add that answer? That is the correct solution and you are the one who "first" posted it. – Mindastic Jul 26 '15 at 18:11
  • Ok, but what do you do then with "hello world!" and length 3? this would return (hel, wor) then instead of (hel, lo , wor, ld!)... – maraca Jul 26 '15 at 18:12
  • @Mindastic because people are downvoting if the answer is too "easy". – maraca Jul 26 '15 at 18:13
  • It's strange I changed \w to . and the code worked :-\ – Divyansh Batham Jul 26 '15 at 18:13
  • @maraca: it doesn't make sense. If your answer is the correct one, i don't see why people would be downvoting it. Add the answer add you will have, at least, one upvote from me :P – Mindastic Jul 26 '15 at 18:14
  • Any help ?? I just figured out that my attempt to add users input to regex was correct other than the my code didn't worked. So how can I mark question as solved?? Because I don't want any downvotes on question?? – Divyansh Batham Jul 26 '15 at 18:16
  • And FYI \\w doesn't work as per my expectations :( I don't want to search '\w' in my string – Divyansh Batham Jul 26 '15 at 18:18
  • @DivyanshBatham if that doesn't work, please add a concrete example of what you want to do because \\w is the correct answer to what you asked. – Mindastic Jul 26 '15 at 18:19
  • @DivyanshBatham What's always working is `[]` so maybe `[A-Za-z0-9_]` ? – maraca Jul 26 '15 at 18:21
  • You should put your entire question and code in your question, not in comments. If you need a 10-comment thread to clarify what you need, there's not enough info in your question. – Jan Jul 26 '15 at 18:23

1 Answers1

2

The problem is that \ is not only the escape character in regex but also in JS strings. So when you create a regular expression from a string you need to escape it. This means that \w becomes "\\w" in a string and if you want to match a single \ it would even become "\\\\".

Instead of changing it to \\w you can also use . if you don't care about the characters or if the string was validated before.

maraca
  • 8,468
  • 3
  • 23
  • 45
  • `\ is not only the escape character in regex but also in JS strings` I didn't knew that earlier, any link where where I can learn about that is much appreciated ^_^ – Divyansh Batham Jul 26 '15 at 18:23
  • @DivyanshBatham I like this source http://www.regular-expressions.info/reference.html, but I usually already know what I'm looking for, should still give a good overview. To *test regex online* there are many sites, just enter the italic text in a search engine. PS: As Jan said, if you put the things you said in the comments in the question it is more likely to get upvoted. – maraca Jul 26 '15 at 18:36