1

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.

1
  • You have tried it with Java Regex? Can you show what you tried? Commented Jan 24, 2014 at 10:15

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi thanks for your answer. But i don't want to get the "" also. I just want the words and the + symbol
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.