2

I have a string like below...

var1=val1 var2=val2 var3="p1 p2 p3 p4" var4="p1 p2" var5=val5

Now how can I replace all the white spaces with underscore only inside "" using RegExp so that the string looks like below...

var1=val1 var2=val2 var3="p1_p2_p3_p4" var4="p1_p2" var5=val5

so that by using .replace('"','').split(' '), I can get an array like below...

Array(
   var1: "val1",
   var2: "val2",
   var3: "p1_p2_p3_p4",
   var4: "p1_p2",
   var5: "val5"
)

no jQuery please...

Naz
  • 2,520
  • 2
  • 16
  • 23

4 Answers4

2

You want to replace each string within the input string by another string by replacing spaces with underscores within the string. You can use a replace with callback to do that:

var inp='var1=val1 var2=val2 var3="p1 p2 p3 p4" var4="p1 p2" var5=val5'

var outp=inp.replace(/"[^"]*"/g, function(x){
  return x.replace(/ /g, '_');
})

// var outp === 'var1=val1 var2=val2 var3="p1_p2_p3_p4" var4="p1_p2" var5=val5'
John Dvorak
  • 26,799
  • 13
  • 69
  • 83
  • Friend, please don't mind... garyh has better answer than you... hope you will understand... – Naz Nov 21 '12 at 10:26
  • @BlackCobra It's up to you to choose the accepted answer. Note that Garyh's answer assumes there's no `=` within the quoted string (and that there _is_ an `=` outside it) – John Dvorak Nov 21 '12 at 10:31
2

I know this has been answered but I thought I'd share this regex using a positive look-ahead. This allows you to replace without using a callback.

var str = 'var1=val1 var2=val2 var3="p1 p2 p3 p4" var4="p1 p2" var5=val5';

str = str.replace(/\s(?=[^=]*")/g, '_');

To explain:

\s        match a space...
(?=       start of positive look-ahead
[^=]*"    ...which is followed by anything except an =, up to a double-quote
)         end of positive look-ahead

Then the g will repeat the search

garyh
  • 2,782
  • 1
  • 26
  • 28
1
var arr = string.replace(/(=")([^"]*)(")/g,function(m,g1,g2,g3){return g1 + g2.replace(/ /g, "_") + g3;}).split(' ');
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Same idea as mine, except I don't need capturing groups ;-) – John Dvorak Nov 21 '12 at 06:49
  • @JanDvorak I don't really need them either, since their values are static. – Asad Saeeduddin Nov 21 '12 at 06:54
  • @asad you answer is incomplete... your code return me `var1=val1 var2=val2 var3="p1_p2 p3 p4 p5 p6 p7 p8" var4="p1 p2" var5=val5`... it replace only the first white space for `var3`... and `var4` is not even touched... and after split the string by `' '`, it returns me a horrible array... please check your code at least once before submit... you almost have completed the code... – Naz Nov 21 '12 at 08:11
  • @BlackCobra Just forgot to add a `g` flag at the end of my regex. Change this: `/(=")([^"]*)(")/` to this: `/(=")([^"]*)(")/g`, and it should work. – Asad Saeeduddin Nov 21 '12 at 08:20
  • @BlackCobra How is this incomplete? – Asad Saeeduddin Nov 21 '12 at 08:59
  • @asad Friend, because now it returning `var1=val1 var2=val2 var3="p1_p2 p3 p4" var4="p1_p2" var5=val5`... have a close look at `p1_p2 p3 p4`... it should be `p1_p2_p3_p4`... – Naz Nov 21 '12 at 09:08
  • @BlackCobra In Firefox and Chrome I get the output array: `['var1=val1','var2=val2','var3="p1_p2_p3_p4"', 'var4="p1_p2"', 'var5=val5']` – Asad Saeeduddin Nov 21 '12 at 09:12
  • @BlackCobra I've made an edit, does this version work correctly in the browser you're using? – Asad Saeeduddin Nov 21 '12 at 09:16
  • @asad check the edit... as i can't edit less that 6 characters, I try to give a nice look to your code... besides, as i already accepted an answer, i have gave a up vote to your answer... – Naz Nov 21 '12 at 09:35
  • @BlackCobra I've incorporated your edit. If you were the one that upvoted all my other answers, please don't do that, that isn't what upvotes are for. – Asad Saeeduddin Nov 21 '12 at 11:19
0

Choose the one you need:

Object:

var txt = 'var1=val1 var2=val2 var3="p1_p2_p3_p4" var4="p1_p2" var5=val5',
    arr = txt.split(" "),
    obj = {};

for(var i = 0 ; i < arr.length; i++){
    var ind = arr[i].match(/^(.+)=/)[1],
        val = arr[i].match(/=(.+)$/)[1].replace(/_/g," ");
    obj[ind] = val;
}

console.log(obj);  //This will give you an object.
// {
//     var1: "val1",
//     var2: "val2",
//     var3: ""p1 p2 p3 p4"",
//     var4: ""p1 p2"",
//     var5: "val5"
// }

Array:

var txt = 'var1=val1 var2=val2 var3="p1_p2_p3_p4" var4="p1_p2" var5=val5',
    arr = txt.split(" "),
    list = [];

for(var i = 0 ; i < arr.length; i++){
    var val = arr[i].match(/=(.+)$/)[1].replace(/_/g," ");
    list.push(val);
}

console.log(list);  //This will give you an array.
// [
//     "val1",
//     "val2",
//     ""p1 p2 p3 p4"",
//     ""p1 p2"",
//     "val5"
// ]
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247