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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    518
    Thanks
    25
    Thanked 0 Times in 0 Posts

    :: urgent string help needed ::

    i have strings that are produced via JS like the one below:

    http://template/Cdatastore.nsf/CVListWeb2/$searchview?searchView&query=FIELD+CVName+CONTAINS+Ben+Richards+AND+&SearchFuzzy=TRUE&SearchWV=True& SearchOrder=4

    i want to be able to remove the last occurance of '+AND+' but there maybe multiple occurances so i cant just do a massive find/replace on them - any ideas peeps? my JS is rusty and i wasnt that good to start with

    need this asap plz peeps for a demo this afternoon
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #2
    Regular Coder
    Join Date
    Mar 2003
    Posts
    176
    Thanks
    0
    Thanked 0 Times in 0 Posts
    off the cuff, try something like this:

    Code:
    str="http://template/Cdatastore.nsf/CVListWeb2/$searchview?searchView&query=FIELD+CVName+CONTAINS+Ben+Richards+AND+&SearchFuzzy=TRUE&SearchWV=True&SearchOrder=4"
    
    strArray=str.split("+AND+")
    alert(strArray[0])
    alert(strArray[1])
    
    strNew=strArray[0] + strArray[1]
    alert(strNew)
    Not fancy but hey...hope that helped.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    518
    Thanks
    25
    Thanked 0 Times in 0 Posts
    nope all that does is remove any instance of +AND+ i just need it to remove the last one but not any previous ones...

    any more ideas?
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could try this:

    var url = 'http://template/Cdatastore.nsf/CVListWeb2/$searchview?searchView&query=FIELD+CVName+AND+foo+CONTAINS+Ben+Richards+AND+&SearchFuzzy=TRUE&Search WV=True&SearchOrder=4';
    document.write(url.replace(/\+AND\+([^+AND+]+)$/, '$1'));

    However, if these URL strings are generated by JavaScript, it could be that it's really easier to prevent the addition of the last +AND+ string instead of later replacing it. Just a thought.
    De gustibus non est disputandum.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    518
    Thanks
    25
    Thanked 0 Times in 0 Posts


    n1 m8 - i had originally started to try and not have the last +AND+ but i am basically creating a search from about 10 select boxes and the amount of if else that i was doing was horendous (prolly cos my JS aint too hot) and this method works a treat!
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    518
    Thanks
    25
    Thanked 0 Times in 0 Posts
    i dont suppose you could explain how that last post works could you cos it looks crazy to me
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay. let's have a close look behind the curtains. It's basically a call to the replace() function of the String object, and works with a regular expression pattern.

    url.replace(/\+AND\+([^+AND+]+)$/, '$1')

    A pattern is used to desribe in a short and concise syntax which substrings are valid matches and which aren't. In our case, the pattern is this:

    /\+AND\+([^+AND+]+)$/

    The string we want to find is '+AND+'. We have to escape the special meaning that is carried with the plus signs with the backslashes. Until yet, we would find any occurence of '+AND+'. But we want to find the last one. So we a second part to our pattern. We would say in english 'if the "'+AND+'" string is not followed by another occurence of itself, then it must be the last one'. That's what our second part implements:

    ([^+AND+]+)$

    ... , LOL, while writing the explanation I notice a bug in my code. Wonder why it works for your case, but it's not correct. The circumflex will negate a pattern, but it only negates each single char, not the sequence of chars. So here is a fixed version:

    url.replace(/(.+)\+AND\+(.+?)$/, '$1$2'

    The dot (.) matches anything, and the + sign allows one or more repetitions. Parentheses stand for grouping. Grouped subpatterns are remembered int the $1, $2 variables and can be used in the replacement string.

    Probably I'm only confusing you further, so I stop my rambling here.
    De gustibus non est disputandum.

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    England =)
    Posts
    518
    Thanks
    25
    Thanked 0 Times in 0 Posts
    thanks m8 - understood every word of that but at least i can follow it thru in future to solve similar problems - much appreciated!!!!!
    "They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson


  •  

    Posting Permissions

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