Jump to content

[SOLVED] exec results not always working..


Dragen

Recommended Posts

grr.. I can't figure out my problem.

I've got a function set to an onclick, which runs am exec search. If the search is successful, it then replaces the found part with.. well. blankness.

function changepic(img, name, val){
var setsearch;
var img_src = document[img].src;

name = name.toUpperCase();

	setsearch = /\W/g;
	result = setsearch.exec(val);
	if(result == null){
		alert(result+' '+val);
	}else{
		val = val.replace(result, "");
		alert(result+' '+val);
	}
}

setsearch is an ereg search for any non-alpha character. If it's null (not in the searched variable) then I alert the result and val.

If it is successful, then I remove the characters from val. And alert the results.

 

Now the problem. The first time I run the script it works fine. The second it comes out as null, third it works, fourth it doesn't etc..

I'm sending the same value each time, and it shouldn't return null for it.

This is how I'm calling it:

<input type="text" name="bg" value="#06d6a6" id="bg" onfocus="javascript:changepic('prev', 'BG', this.value);" />

so it should replace the hash (#) each time, but for some reason it does it every other time..

 

Any ideas why it's doing this?

 

Thanks

Link to comment
Share on other sites

again.. solved ::)

 

The problem was that I was using setsearch as a simple variable:

setsearch = /\W/g;

when I should have been setting it as a RegExp:

setsearch = new RegExp(/\W/g);

Which was giving me the strange result of working exactly every other time...

 

heh.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.