We help IT Professionals succeed at work.

javascript regular expression assign everything after underscore to a variable

Hello!

I'm trying to create a super simple regular expression! I just want to assign to a variable everything after an underscore. it will be a counter- number, then and underscore then a fedex or ups tracking code.

var linkId = 3_1Z6656460348661915;

//incomprehensible regular expression magic 

var trackingNumber = 1Z6656460348661915;

Open in new window


I'm grateful for any help! Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Try:

var trackingNumber = /_(.*)/[1];

Open in new window

Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
var linkId = 3_1Z6656460348661915;
var trackingNumber = linkId.split("_")[0]; // faster than regex
//or with a regex
//var trackingNumber = linkId.match(/_(.*)/)[1]; // kaufmed's illustration ?

Open in new window

Author

Commented:
thanks!
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
you're welcome!

var linkId = 3_1Z6656460348661915;
var trackingNumber = linkId.split("_")[1]; // ^_^

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.