SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    extracting specific characters from an inputted string into a variable

    Hi there

    I have a little function that validates whether a string entered is syntactically correct

    Code:
    function str_validate() {
                            var patt=/^[a-z]{3}w(prd|uat|dev)\d{1}$/;
                            val = document.getElementById("hostname").value;
                            if ((val.length > 7) && patt.test(val)) {
                                       // do some good stuff
                            } else {
                                    alert("Invalid string Name");
                                    ClearForm();
                            }
                    }
    }

    as you can see I am matching against 8 characters made up of 3 alphas, a 'w', a three character string of 'prd', 'uat' or 'dev' and then a single numeric

    what i am looking to do is to extract into a variable the three character string ('prd', 'uat' or 'dev') so that i can use it to work with in another function ...

    does anybody know how I would be able to extract this part of the string (its always the 5th,6th and 7th character of the 8 charachter string)

    any help would be greatly appreciated

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looking around some more would back references be what im looking for (str.replace) ?? ... it seems to only examples i can find of this are to replace parts of a string not to extract parts ?

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    got it .... (if anyone's remotely interested )

    var str = "abcwuat1";
    var newstr = str.replace(/[a-z]{3}w((uat|prd))[1-9]{1}/,"$1");
    document.write(newstr);


    prints...

    uat


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •