Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get substring value from main string?

I have string similar to this one.

HTML

var str = "samplestring=:customerid and samplestring1=:dept";

JS

var parts = str.split(':');
var answer = parts;

I want to trim substrings which starts with colon: symbol from the main string

But it is returing the value like this

samplestring=,customerid and samplestring1=,dept

But I want it something like this.

customerid,dept

I am getting main string dynamically it may have colon more then 2.

I have created a fiddle also link

like image 737
Shrinivas Pai Avatar asked Dec 05 '22 20:12

Shrinivas Pai


2 Answers

var str = "samplestring=:customerid and samplestring1=:dept";
alert(str.match(/:(\w+)/g).map(function(s){return s.substr(1)}).join(","))
like image 200
Qwertiy Avatar answered Dec 24 '22 05:12

Qwertiy


you can try regex:

var matches = str.match(/=:(\w+)/g); 
var answer = [];

if(matches){
    matches.forEach(function(s){
        answer.push(s.substr(2));
    });
}
like image 45
xianyu Avatar answered Dec 24 '22 05:12

xianyu