简体   繁体   中英

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

You can use split function:

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

Or match :

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

You can use . pop() function.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM