How to get substring value from main line?

I have a line like this.

HTML

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

Js

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

I want to trim substrings that start with a colon : from the main line

But it retains a meaning like this

 samplestring=,customerid and samplestring1=,dept 

But I want something like this.

 customerid,dept 

I get the main line dynamically, it can have a colon greater than 2.

I created a fiddle also a link

+4
source share
8 answers

 var str = "samplestring=:customerid and samplestring1=:dept"; alert(str.match(/:(\w+)/g).map(function(s){return s.substr(1)}).join(",")) 
+5
source

you can try regex:

 var matches = str.match(/=:(\w+)/g); var answer = []; if(matches){ matches.forEach(function(s){ answer.push(s.substr(2)); }); } 
+3
source

Here is a single line:

 $.map(str.match(/:(\w+)/g), function(e, v) { return e.substr(1); }).join(",") 
+2
source

You can try it like

 var str = "samplestring=:customerid and samplestring1=:dept and samplestring11=:dept"; var results = []; var parts = str.split(' and '); $.each(parts, function( key, value ) { results.push(value.split(':')[1]); }); 

The results array now contains three values โ€‹โ€‹of customerid , dept and dept

+1
source

Try

 var str = "samplestring=:customerid and samplestring1=:dept"; var parts = str.split(':'); var dept = parts[2]; var cus_id = parts[1].split(' and ')[0]; alert(cus_id + ", " + dept ); 

Using this, you will get o / p like: customerid,dept

+1
source

it will give you what you need ...

 var str = "samplestring=:customerid and samplestring1=:dept"; var parts = str.split(' and '); var answer = []; for (var i = 0; i < parts.length; i++) { answer.push(parts[i].substring(parts[i].indexOf(':')+1)); } alert(answer); 
+1
source

 var str = "samplestring=:customerid and samplestring1=:dept"; alert(str.replace(/[^:]*:(\w+)/g, ",$1").substr(1)) 
+1
source

Here \S , where S is capital, you need to get non-whitespace characters so that it gets the word before the first space to match it, so it will match the word after : to the first place, and we use /g to not only match the word fisrt and continue searching in line for other matches:

 str.match(/:(\S*)/g).map(function(s){return s.substr(1)}).join(",") 
+1
source

All Articles