1
var objname = "Image1-123456-789.png"

quick question i wanted to split this text without match them together again.

here is my code

var typename = objname.split("-");

//so it will be Image1,123456,789.png

var SplitNumber = typename[1]+'-'+typename[2];
var fullNumber = SplitCode.split('.')[0];

to get what i wanted

my intention is to get number is there anyway i can split them without join them and split again ?

can a single code do that perfectly ? my code look like so many job.

i need to get the 123456-789.

3 Answers 3

3

The String.prototype.substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.

This method extracts the characters in a string between "start" and "end", not including "end" itself.

var objname = "Image1-123456-789.png";
var newname = objname.substring(objname.indexOf("-")+1, objname.indexOf("."));
alert(newname);

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

1 Comment

thank is what i needed ~ have a beautiful day for you.
0

An alternate can be using Join. You can use slice to fetch range of values in array and then join them using -.

var objname = "Image1-123456-789.png";
var fullnumber = objname.split("-").slice(1).join("-").split(".")[0];
alert(fullnumber)

Reference

Join Array from startIndex to endIndex

Comments

-2

Here is your solution

var objname = "Image1-123456-789.png";
var typename= objname.split("-");


var again=typename[2];
var again_sep= again.split(".");

var fullNumber =typename[1]+'-'+again_sep[0];

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.