Sush
Sush

Reputation: 1457

get particular value from a string using javascript

using javascript how can i get 0e783d248f0e27408d3a6c043f44f337c54235ce from below string

my console.log(stdout); prints

 status.html:commit 0e783d248f0e27408d3a6c043f44f337c54235ce

The value 0e783d248f0e27408d3a6c043f44f337c54235ce will be changing.

Am trying to find that key

Upvotes: 0

Views: 189

Answers (3)

Nishu Tayal
Nishu Tayal

Reputation: 20880

You can use .pop() function.

var str = "status.html:commit 0e783d248f0e27408d3a6c043f44f337c54235ce";
str.split("commit").pop().trim()

Upvotes: 1

griffon vulture
griffon vulture

Reputation: 6774

You can use split function:

stdout.split("status.html:commit ")[1]

Or match:

stdout.match("status.html:commit (.*)").pop()

Upvotes: 1

adeneo
adeneo

Reputation: 318372

If commit is a constant, and you just need the rest of the string from commit to the end, you can split the string on commit and pop of the last part

string.split('commit').pop();

It will leave an extra space, but you can either trim that off, or just add a space in

split('commit ')
//           ^ space

Upvotes: 1

Related Questions