Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of regular expressions to simulate *

Status
Not open for further replies.

DannyLondon

Programmer
Nov 19, 2002
33
GB
Hello All

I have wracked my brain trying to understand regular expressions and trying to solve a problem that I do not believe should be difficult. Alas, I have still not found a solution.

I have the following two strings, which are defined at run time:
var pet = "dog"
var animals = "dog 1;cat 3;horse 1;lion 2"

I would like to replace the "dog 1;" in the second string with an empty string, based on the fact that "dog" is in the first string. I know each animal will be separated by a semi-colon, but don't know what will be between the animal and the semi-colon. In pseudo code, this would be:
var result = animals.replace(pet + "*;", "")

Painfully, I cannot find the code for this pseudo-code. Any help that people could offer would be greatly appreciated.

Thanks, Danny...



 

i don't understand exactly...can you explain the purpose of the code...as in its function...also, how come the pet default is dog? is that necessary? and why could there be anything in front of the pet name?

explain a little more in depth please.

- g
 
Hi Spewn

Thanks for the reply. My code is not actually looking at animals, I tried using that as a more simple example of what I was trying to achieve.

I have a form with a large number of controls on it. I am using a single text box to track all of the changes that have been made, before passing them back to the server to save the changes.

Using the text of my previous example, "dog" would have been the name of the control, and "1" the value in it. In the example, changes have also been made to controls named "cat", "horse" and "lion".

Each time the user changes the value in a control, I want to check if a change is already in the list for this control. To find it, I know the control name, value and semi-colon will be in the list. I am therefore trying to replace this string with an empty string. Finally I append a new string for the control with its new value onto the end of all of the other changes.

eg.
Changes currently: "dog 1;cat 3;horse 1;lion 2"
User changes control horse to 28
Changes become "dog 1;cat 3;lion 2;horse 28"

I would be very grateful if you have a solution.

Thanks, Danny...
 
try this:
[tt]
var pet = "dog";
var animals = "dog 1;cat 3;horse 1;lion 2";
var oRE = new RegExp("\\b" + pet + "[^;]+;?","gi");
alert(animals.replace(oRE, ""));
[/tt]

=========================================================
if (!succeed) try();
-jeff
 
Hi Jeff

Thank you very much for your help. I can see why I was never going to be able to work that out.

Danny...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top