Question

Given the following string: 's0\\8\\26\\29\\30\\32' or 's0\\8\\26\\' or 's0\\5', I need to return the last digits of the string.

Given:

 function getFolderID(mystr) {
        var reFolderID = /\bs0\\\\[0-9]+\b/g //regexp to return s0\\34
        var retArr = [];
        var retval = '';

        retArr = mystr.match(reFolderID);
        retArr = retArr[0].replace(/s0\\\\/g, "");

        if (retArr != null) {
            retval = retArr[retArr.length - 1];
        }
        //alert("Ret: " + retval);
        return retval;
    }  

At first I thought I just needed the first digits, but turns out I need the last ones.

What would the proper regexp term be for this?

Also, how can I create an ASP.Net event handler to do something with the returned JS value?

Was it helpful?

Solution

You might try the following. It should following the formatting and group the last set of numbers.

/s0\\\\(?:[0-9]+\\\\)*([0-9]+)/

So, something like:

function getFolderID(mystr) {
    // search string for last group of digits in the pattern
    var matches = mystr.match(/s0\\\\(?:[0-9]+\\\\)*([0-9]+)/);

    // if matches is null, replace with "defaults"
    matches ||= ["", ""];

    // grab the first grouped match
    return matches[1];
}

As for the ASP.NET event, you'll probably have to use Ajax -- such as by <asp:UpdatePanel /> or your choice of Ajax library (jQuery, Prototype, etc.).

Without Ajax, JavaScript and ASP.NET will never execute at the same time.

OTHER TIPS

You could match the last digits in a string this way:

function getFolderId(str) {
    var pattern = /([0-9]+)$/;
    var result = pattern.exec(str);
    return result ? result[1] : null;
}

On the issue of returning Javascript parameters to the server...

I created two ASP.Net hidden fields, then in JS, set the values of these fields... voila - available from my code-behind:

    // set .net hidden control values so they're server-side accessible
    document.getElementById('ctl00_ContentPlaceHolderMainBody_HidTreeContextAction').value = buttonAction;
    document.getElementById('ctl00_ContentPlaceHolderMainBody_HidTreeContextID').value = buttonFolder;

And

<asp:HiddenField ID="HidTreeContextAction" runat="server" />
<asp:HiddenField ID="HidTreeContextID" runat="server" />

Now the workflow is: 1) user right-clicks on tree node 2) context menu appears 3) selecting a context menu item fires the JS that sets the hidden values 4) ajax-style window appears to accept user input 5) user clicks submit 6) both parameters are used do determine what/where the data goes

I realize this may be a bit of a hack, and not using AJAX. If someone cares to share how this would be done ajax-style, great!

Just thought I'd share this quick fix in case anyone else needs it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top