-3

I have my data set in a normal data model format:

var data = "Billing, alpha, beta"

But I want to modify this data by formatting the array by replacing "," with "/text/i"

var newData = [/Billing/i , /alpha/i, /beta/i]

Here is the code I have tried:

var data   = "Billing, alpha, beta"
var y = x.split(',')
var newData  = y.replace(",", "/i,")
console.log(newData)
6
  • what is the expected output? Commented Jul 19, 2017 at 9:32
  • This might work if you remove y and just run the replace on data. Commented Jul 19, 2017 at 9:33
  • var newData = [/Billing/i , /alpha/i, /beta/i] Commented Jul 19, 2017 at 9:33
  • jsfiddle.net/v2g1a72a Commented Jul 19, 2017 at 9:34
  • @WiktorStribiżew the output contains space on from second string onwards Commented Jul 19, 2017 at 9:38

1 Answer 1

-1

Try this snippet

var data   = "Billing, alpha, beta";
var y = data.split(',');
newData=[];
for(i=0;i<y.length;i++)
  newData.push(new RegExp( y[i].replace(/(^\s+|\s+$)/g,""),"i"));
console.log(newData);
Sign up to request clarification or add additional context in comments.

6 Comments

he output contains space on from second string onwards
@SaurabhRaman Please accept my solution if you are satisfied
The output is an array of strings, while OP needs an array of RegExp objects.
I appreciate your knowledge many thanks for your help....but there is little correction the output should also not contain ' " ' (double quotes)..... it should look like .... [/Billing/i,/alpha/i,/beta/i]
See my edit I've used Regex objects
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.