Sathesh S
Sathesh S

Reputation: 1323

Javascript regex to find and concatenating the particular strings?

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

Answers (2)

Sujith PS
Sujith PS

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);

DEMO

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);

Demo

Upvotes: 0

anubhava
anubhava

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"

UPDATE:

s.replace(/.*?"([^"]*)"([^"]*)/g, function($0, $1, $2) {return $2!=""? $1+'+':$1});
//+> Java script+Regex

Upvotes: 3

Related Questions