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.
-
You have tried it with Java Regex? Can you show what you tried?putvande– putvande2014-01-24 10:15:45 +00:00Commented Jan 24, 2014 at 10:15
Add a comment
|
2 Answers
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"
UPDATE:
s.replace(/.*?"([^"]*)"([^"]*)/g, function($0, $1, $2) {return $2!=""? $1+'+':$1});
//+> Java script+Regex
1 Comment
Sathesh S
Hi thanks for your answer. But i don't want to get the "" also. I just want the words and the + symbol
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);