View Full Version : :: urgent string help needed ::

07-10-2003, 01:15 PM
i have strings that are produced via JS like the one below:


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

07-10-2003, 01:36 PM
off the cuff, try something like this:



strNew=strArray[0] + strArray[1]

Not fancy but hey...hope that helped.

07-10-2003, 01:41 PM
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?

07-10-2003, 01:53 PM
You could try this:

var url = 'http://template/Cdatastore.nsf/CVListWeb2/$searchview?searchView&query=FIELD+CVName+AND+foo+CONTAINS+Ben+Richards+AND+&SearchFuzzy=TRUE&SearchWV=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.

07-10-2003, 02:34 PM

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!

07-10-2003, 02:40 PM
i dont suppose you could explain how that last post works could you cos it looks crazy to me :)

07-11-2003, 12:15 AM
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:


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:


... :D, 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.

07-11-2003, 09:58 AM
thanks m8 - understood every word of that ;) but at least i can follow it thru in future to solve similar problems - much appreciated!!!!!