Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13

Thread: regExp

  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts

    regExp

    Hello.

    Is there a regExp that will find the position of the last alphabetical character in a string.

    In this example the last character would be d

    str="hello123world456"

    Thanks
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this.
    Code:
    var idx = str.search(/[a-z][^a-z]*$/i);
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Thanks Kravvitiz, that sure beats looping through the string
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Sorry to post again but what would the expression be to find the last non numerical character in the string

    I have tried

    var idx = str.search(/[^0-9]*$/)
    Last edited by Mr J; 08-05-2006 at 01:47 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    off the top of my head, i believe that'd be
    var idx = str.search(/\D\d*$/i);
    Last edited by joh6nn; 08-05-2006 at 02:02 PM.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Hi Joh6nn and thanks.

    var idx = str.search(/\D*$/i);

    returns the index of the end of the string, I also tried

    var idx = str.search(/[^0-9]/);

    but that returned zero

    basically what I am after is getting the number at the end of a string

    string123

    My last try was

    var idx = str.search(/[0-9]*$/)

    which does appear to work but I do not know enough about regExp to know if it is valid
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i made a typo in my first post, and edited it, but looks like only after you'd already seen it.

    the solution you came up with looks like it should work, but you should test in thorougly, just in case. if you're new to REs, Liorean has a pretty good tutorial at evolt.org.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #8
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This finds the index of the first numerical character of any numerical characters at the end of the string.
    Code:
    var idx = str.search(/[0-9]*$/)
    What you want is what joh6nn posted, which could also be written like this:
    Code:
    var idx = str.search(/[^0-9][0-9]*$/i);
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Kravvitz
    This finds the index of the first numerical character of any numerical characters at the end of the string.
    Code:
    var idx = str.search(/[0-9]*$/)

    I have tried this and get the folllowing:-

    abc123 returns 3

    abc123xyz returns 9

    123abc returns 6

    123abc789 returns 6

    a returns 1

    0 returns 0

    So the regex returns the index of the last alpha character in the string.

    Something wrong here??? WIN98SE and IE5.5
    Last edited by Philip M; 08-05-2006 at 09:24 PM.

  • #10
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Compare those to what you get with the RegExp that joh6nn posted there's a difference of 1.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #11
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    It looks like both could be used to get the result I want.

    The method Joh6nn posted

    var idx = str.search(/\D\d*$/i);

    works and gets the index of the last alpha character whereby any remaining numbers in the string can be found, hence

    str="abc123"

    result=str.substring(idx+1,str.length)

    On the other hand

    var idx = str.search(/[0-9]*$/)

    finds the beginning of the numbers at the end of the string

    result = str.substring(idx,str.length)

    both display 123
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #12
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If that's what you want to do with it, then it doesn't matter.

    My point is that those two RegExp produce different results.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #13
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    I am happy to use either and I would like to thank you all for your time and help
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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