Javascript href help

HeirHeir Registered User regular
edited April 20 in Help / Advice Forum
Hey all,

I haven't really had to do any kind of scripting/coding in over a decade, so I'm super rusty.

I'm writing a simple function that will check for the presence of any length string of numbers at a certain point in a url. I can use the href attribute to see if the
href == "something.com/requests/1234"
, but not sure how to write it to check for something like
href == "something.com/requests/[NUMBERS HERE]"

I'm assuming some RegEx might be in order...but that's the devil's tongue, and I woefully do not speak it. Could I get some help? Would RegEx be the best approach anyways?

Other points:

1. There will never be anything after that set of numbers.
2. I need to be able to capture that set of numbers to store in a variable.

camo_sig2.png
Heir on

Posts

  • xraydogxraydog Registered User regular
    edited April 20
    Do you have to support IE? If you don't a URL object can do this: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
    let url = new URL("something.com/requests/1234");
    let parts_of_path = url.pathname.split("/");
    

    xraydog on
    Baron Dirigible
  • GarthorGarthor Registered User regular
    edited April 20
    The question is somewhat underspecified. A regex would probably be the "right" way to do it, but it might be a bit of an overengineered solution. Nevertheless, you would do it as so:
    var pattern = /[0-9]+$/;
    var result = pattern.exec(href);
    

    The regular expression is /[0-9]+$/, and it matches any character in the brackets (0-9, so any digit) 1 or more times (the +), followed by the end of the string (the $ sign). So, any string ending with a series of numbers will return that series of numbers, otherwise it will return null.

    More on regular expressions in Javascript can be found at https://www.w3schools.com/js/js_regexp.asp

    Garthor on
  • HeirHeir Registered User regular
    Thanks Garthor!

    My other solution was to use Slice() as the first set of characters will always be the same, so I could slice off the first 49 characters or whatever the count was. But I still need to make a check to ensure I'm on the right page before I run the above script.

    The problem is that there can be other pages that have a url like
    something.com/requests/new?formid=23234
    

    But on an existing request page (a ticket that has already been submitted, the format will always be
    "something.com/requests/[NUMBERS HERE]"
    

    Any recommendations on checking for that specific format in the url before running any script?

    camo_sig2.png
  • GarthorGarthor Registered User regular
    edited April 21
    So, going into slightly more complicated regex, you can do this with a single evaluation, but you need to use groups. The regex would look like this:
    var pattern = /something.com\/requests\/([0-9]+)$/
    

    Note the escaped-out forward slashes. This will match the literal "something.com/requests/" part of the string. This will return the entire string while matching, though, so groups need to be used to extract the part you are interested in. The parenthesis indicate a group, and the returned value is an array (actually it was always an array, I'm not great at javascript or I would've known to point that out) containing first the entire match, and then the matches for each group. Element 0 (result[0]) is the entire matching string, so "something.com/requests/12345", and our only group ([0-9]+) is element 1 (result[1]), which will be "12345".

    Garthor on
  • HeirHeir Registered User regular
    Thanks again Garthor.

    camo_sig2.png
Sign In or Register to comment.