Reputation: 1323
This is a demo from "Java script". This Demo uses "Regex"
from the above string i want to take the words between ""
and concatenate it.Between Both the words i want to put +
symbol. I tried with java regex but i want to do it with Javascript Regex. My final answer should be Java scripr+Regex
.
Upvotes: 1
Views: 56
Reputation: 4864
Somply you can use this :
var s='This is a demo from "Java script". This Demo uses "Regex"';
var reg=/\"(.*?)\"/g;
var result=s.match(reg).join('+').replace(/"/g,"");
console.log(result);
OR
You can use something like this :
var s='This is a demo from "Java script". This Demo uses "Regex"';
var reg=/\"(.*?)\"/g;
var result="";
s.match(reg).forEach( function(a){
if(result!="")
result+="+";
result+=a.replace(/\"/g,"");
});
console.log(result);
Upvotes: 0
Reputation: 786359
You can use:
var s = 'This is a demo from "Java script". This Demo uses "Regex"';
var r = s.match(/"([^"]*)"/g).join('+');
//=> "Java script"+"Regex"
s.replace(/.*?"([^"]*)"([^"]*)/g, function($0, $1, $2) {return $2!=""? $1+'+':$1});
//+> Java script+Regex
Upvotes: 3