2

My title might not be worded correctly sorry in advance. I've looked everywhere and I honestly can't seem to figure it out. I want to use a variable in a RegEx but the solution that i've found and tried to work off of works but it is not flexible for what I need. I can't seem to figure out how to convert it to a RegEx constructor object which I think is my best bet. Any help will be greatly appreciated.

let str = 'haeandviaecy'

let newString = str.replace(/(\w{3})/g, '$1 ').replace(/(^\s+|\s+$)/, '')
//hae and via ecy

replace(/(^\s+|\s+$)/,'') removes any preceding or trailing space from the string - just giving you a heads up

So what this snippet does is go to every third and put a blank space in it. What I want is to make my code more flexible and be able to put a variable to let me choose the number for when it puts a blank space using a variable.

right now it prints out - "hae and via ecy" for ex. every 4th would be - haea ndvi aecy

I've read you cant put in variables unless it is a RegEx contructor object. When I've tried to convert it doesn't seem to work.

  • 2
    Why are people voting to reopen this? It's a clear duplicate. – Robby Cornelissen Feb 22 '19 at 03:49
  • @RobbyCornelissen FWIW Did not vote for "close" or "reopen". Some users do not automatically search for duplicate questions and answers for every question before posting an answer. A substantial number of _new_ questions at SO could be marked as a duplicate based on any number of arbitrary or specific criteria. Some users answer questions that do not include a clear problem statement, that is, code that "works". It depends on the context. And individual user choice. There are is "standard" at any SE site any other user is obliged to acknowledge or endorse as their own. – guest271314 Feb 22 '19 at 04:02
  • @guest271314 I am not faulting the OP for asking a duplicate question, especially since the user is new to Stackoverflow. I'm just wondering why two veteran users have voted to reopen a question that is a clear duplicate. – Robby Cornelissen Feb 22 '19 at 04:07
  • @RobbyCornelissen Cannot speak for any other user. Ask them. A similar rationale can be used to ask why this question https://stackoverflow.com/posts/comments/96414303 was not posted at or migrated to [Code Review](https://codereview.stackexchange.com/) due to the fact that the code "works" and the only actual question is based on opinion. `JSON.parse(JSON.stringify(input))` resolves the inquiry as to "mutation". Nonetheless, more than one user decided to answer the question. Perhaps they found the question interesting? Not sure. The criteria at SO is for code that does not work – guest271314 Feb 22 '19 at 04:12
  • 1
    @guest271314 *"Note sure why any other user does whatever they do. Ask them."* That's exactly what I did. – Robby Cornelissen Feb 22 '19 at 04:14
  • @RobbyCornelissen That is SE culture. And a relatively tame expression of that culture. SE user is not obligated to state why they cast "up", "down", or "delete" vote. That stops well short of _one_ of the most (but not limited to) egregious expressions of SE culture: censorship. – guest271314 Feb 22 '19 at 04:17

1 Answers1

-2

If gather the question correctly, you can use a template literal as parameter passed to RegExp constructor.

guest271314
  • 1
  • 15
  • 104
  • 177
  • @DavisUmana E.g., `let str = 'haeandviaecy'; let n = 5; let re = new RegExp(\`(\\w{${n}})\`, "g") // note escape of \w; let newString = str.replace(re, '$1 ').replace(/(^\s+|\s+$)/, '') console.log(re, newString); // "haean dviae cy"` – guest271314 Feb 22 '19 at 03:56
  • Might be a typo in it I think. I'll see if I can find your error. Looks like it will work. I appreciate it and let you know if it does. – Davis Umana Feb 22 '19 at 04:03
  • @DavisUmana What do you mean by _"might be a typo"_? There is no error in the code. _"Looks like"_ is not applicable to code. Code is not based on emotion or opinion. Code is based on input and output. Reproducible results. Code needs to be tested to verify reproduction of output http://plnkr.co/edit/dSbT32vOhU0zmiGluA10?p=preview – guest271314 Feb 22 '19 at 04:06
  • Sorry about that. I must have messed up the copy/paste. It works just how I need it to. I appreciate the answer and the time you took to help me resolve it. – Davis Umana Feb 22 '19 at 04:18