views:

80

answers:

5

For some reason, I can't seem to find a good answer for this one.

I have been trying to escape out the caret (\^), and to use the hex, octal, and other codes for the character using \xdd, \dddd, etc...

But my replace regexp won't replace the caret (^) with anything. It seems to simply break the expression.

Here is the code I am using:


var field,myExp;    

// \x5E is supposed to represent the caret in Hex...
    myExp = / *[^a-z^A-Z^0-9\s\x5E]/gi;

field = field.replace(myExp,"");
alert(field);

Help!

+1  A: 

A character group beginning with ^ is an exclusionary group, and will match every character that isn't in the [].

If you're trying to remove any letter, number, or ^, change the regex to

myExp = / *[a-zA-Z0-9^\s]/gi;
SLaks
Thanks, while I am already aware of that fact, there are times when the user may inadvertently enter it into a form field.I want to make sure that my field contains ONLY letters and number, no spaces, punctuation, or other special characters of any kind.It is more efficient to exclude a small list containing what I want to keep than it is to list an exhaustive list of all items I want to remove....
exoboy
A: 

When you have the caret as the first character in a [] set, it means "not" - but only at the start. So your regexp means "(spaces followed by) anything that's not a-z, or caret, or A-Z, or caret, or 0-9 etc". Remove all but the first caret and you may have more luck :-)

psmears
Actually, it means excluding, but I know what you mean. Thanks for the reply!
exoboy
A: 

Are you trying to replace or keep all a-z, A-Z, 0-9, whitespace, and carats?

If you're trying to keep them, only use one ^ at the very beginning of the expression like so:

[^a-zA-Z0-9\s^]

If you're trying to replace them all including carat, use:
[a-zA-Z0-9^\s]

Edit (updated answer in response to comment):

Use
[^a-zA-Z0-9]
to match and replace all characters that are not a-z, A-Z, 0-9.

Note: You should use the same expression server-side to validate these form fields, as people could have javascript disabled, or just mess with the POST value to mess with your database.

Rob
I want to keep ONLY a-z,A-Z,0-9. I want to remove/replace all other characters.
exoboy
Use `[^a-zA-Z0-9]` That matches anything that is NOT (^) a-z,A-Z,0-9. So the .replace will replace anything that is matches (which is not a-zA-Z0-9).
Rob
A: 

The code snippet you gave is rather confusing, but based on the title of the question, if you just want to replace the character ^ with something else, that can be achieved like this...

var str1 = "test^123";
var str2 = str1.replace(/\^/g, "\x005E");
alert(str2);
Josh Stodola
Sorry about that, I forgot to give a sample value for "field".However, I think I have it figure out. Thanks for responding!
exoboy
A: 

I found the answer, but you guys all helped me get there. Thanks!

I think what was happening was that my exlude (^) was used too many times and so was creating an exclusion of my exclusionary groups... Since there were no separators between the groups, the first one does the trick.

ORIGINAL: repExp = / *[^a-z^A-Z^0-9]/gi;

FINAL REGEXP: repExp = / *[^a-zA-Z0-9]/gi;

The above filters out anything that is not a leter (a-zA-Z) or number (0-9) from a string.

Thanks, people!

P.S. The space after the initial "/" is there because for some reason, Dreamweaver sees it as the beginning of a comment. :-(

exoboy
RegExp only uses a `^` *right* next to a `[` as an exclusionary group, so your carats weren't "canceling each other out". Instead, you had a ^ inside your KEEP group. Because your regexp started as `[^`, anything inside the `[^...]`' would be kept (including `^`'s). Your second expression is saying, keep a-z,A-Z,0-9. your first expression is saying keep a-z,^,A-Z,^,0-9. So it kepes ^'s
Rob
The `/i` modifier in `/pattern/gi` says to ignore case, so you don't need `[^a-zA-Z0-9]` -- simply `/*[^A-Z0-9]/gi` should do. I also wonder about the leading `/*` -- shouldn't it be *dot* star `/.*[A-Z0-9]/gi`? I use too many dialects of regex, so could be confusing them.
Stephen P
Good point about the case-insensitive flag. I know it is redundant, but on occassion, I forget to set the case flag... so having it work either way is just a bonus for those of us who are absent-minded.
exoboy
Dreamweaver sees `/[]/` as a comment?
PP