0

Apologies if this has been asked before but I couldn't find an answer.

If I have a string as follows

 var myString = "/text/text/text/text/TEXT I REQUIRE/text.text";

How would I grab the TEXT I REQUIRE? It will always be between the last 2 forward slashes but the amount of slashes and other text could vary.

Many thanks,

Clint

2
  • 5
    You can split it to get an array and target the arr length -2 index: jsfiddle.net/vsgodvt2 Commented Oct 11, 2016 at 10:10
  • And this is probably more elegant: jsfiddle.net/7nnbgyoL Commented Oct 11, 2016 at 10:18

2 Answers 2

3

first you split the string to array then select element number:

var myString = "/text/text/text/text/TEXT I REQUIRE/text.text";
var res = myString.split("/"); 
var result = res[res.length-2];

and fiddle link is show result on a element.

Sign up to request clarification or add additional context in comments.

Comments

2

var myString = "/I/am/a/beautiful/TEXT I REQUIRE/man.example";
myString=myString.split("/");
console.log(myString[myString.length-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.