0

the data is like this /Canada/Quebec/Montreal or /Japan/Osaka/ or /USA/California/Sanfransico

and I want to extract only country name which is right after first /.

The result should be Canada, Japan and USA

Thank you.

0

4 Answers 4

3

You can split the string on slash and take the second item:

var country = data.split('/')[1];

You can also locate the second slash and use substr:

var country = data.substr(1, data.indexOf('/', 1) - 1);
Sign up to request clarification or add additional context in comments.

Comments

2

Your substring operation would look something like this:

var data = '/Canada/Quebec/Montreal';
var country = data.substring(1,data.indexOf('/',1));

So basically take the string, exclude the first character up to the next occurance of / after the first index.

Comments

2
var thestring = '/Canada/Quebec/Montreal';

console.log(thestring.split('/')[1]);

Comments

0
var country = str.slice(1, str.indexOf('/', 2))

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.