Link to home
Start Free TrialLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

String Manipulation with JS

Hi Experts,

Can anyone tell me how to search for end extract string paths of a string using JavaScript?

For example, say you have a string like ABC{23}, I want to extract this as "ABC" as str1 and "23" as str2. Here is my pseudo-code for this:

pos = strpos(str0, "{")
str1 = substr(str0, 0, pos) //get everything until "{"

str0 = substr(str0, pos+1) //get everything after "{"
pos = strpos(str0, "}"
str2 = substr(str0, 0, pos) //get everything until "}"

Any help will be appreciated.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
and an alternative way would be using Javascript and regular expressions:
var source = "ABC{23}";
var myRegexp = /(\w*)\{(\w*)\}/g;
var match = myRegexp.exec(source);
alert(match[1]);  // ABC
alert(match[2]);  // 23

Open in new window

http://jsfiddle.net/EE_RainerJ/dtsja7bd/

HTH
Rainer
No points please, as I not not posting answer to your question.

But bookmark this link about split function. Really nice reference and examples


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
Avatar of APD Toronto

ASKER

Thanks.