|
JavaScript Regex Question
08-11-2011, 11:37 AM
|
JavaScript Regex Question
|
Posts: 919
Name: Scott Kaye
Location: Ontario
|
Hello JS Forum (again!)
I'm having some trouble working out a problem dealing with string manipulation. I have an input box, and it replaces whatever the user types with x's and 1's. Letters are turned to x's and numbers to 1's. Spaces are kept.
But I want to extend this script, and offer users to keep certain parts of the string intact. So if they type 'h4llo' it will be replaced to 'x1xxx', but if they type 'h[4]llo' it will be replaced to 'x[4]xxx'. I just need to know how to get the script to not replace characters in between the [ and ]'s. Here is my JS (one line)
Code:
return val.replace(/[a-zA-Z]/g,'x').replace(/[0-9]/g,'1').replace(/[^a-zA-Z 0-9 \[\]]+/g,'');
Currently I have the non-alphanumeric replacement excluding [ and ], so they aren't removed like any other characters like !@#$%^&*(). The PHP is what actually does something with the value, but I know PHP enough to tell it to work with just x's and leave everything else as it is.
Thanks!!
-PG
|
|
|
|
08-11-2011, 11:59 AM
|
Re: JavaScript Regex Question
|
Posts: 246
|
Let me first say that I like regex. However, if you write a Regex that is insanely hard to understand it will be insanely hard to modify later as well.
I think you should do what you've done but include other string manipulation techniques without the use of Regex. So basically mix Regex and standard string manipulation in order to create something that works and is easy to read.
|
|
|
|
08-11-2011, 12:59 PM
|
Re: JavaScript Regex Question
|
Posts: 919
Name: Scott Kaye
Location: Ontario
|
I'm not sure what you mean, stbuchok. You want me to use a combination of regex and other kinds of string manipulation?
I suppose that could work... By splitting by [ and ] I could grab what is between those two array keys and not replace that... But I was hoping more of a regex-based answer; something I could literally 'pop' on the end of my already written regex.
Like this:
/[^!(\[|\])$]/g
Meaning 'Not [ or ]', but that doesn't work, or I'm just putting it in the wrong place (in a separate replace() after the others)
|
|
|
|
08-11-2011, 01:56 PM
|
Re: JavaScript Regex Question
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
The ^ can be used as a not-operator, as in
[^0-9]
which will match everythign except digits 0-9. In your case, i believe this should do the trick.
[^\[.+\]]
which will match everything except any charcter(s) enclosed in brackets.
I havn't looked at your code at all, but I'm sure you can work it in there.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
08-11-2011, 03:02 PM
|
Re: JavaScript Regex Question
|
Posts: 919
Name: Scott Kaye
Location: Ontario
|
Thanks for the reply, lizciz! Here is my code:
JS:
Code:
function replace_chars(val) {
return val.replace(/[a-zA-Z]/g,'x').replace(/[0-9]/g,'1').replace(/[^a-zA-Z 0-9 \[\]]+/g,'');
}
HTML Code:
<input type="text" onchange="this.value=replace_chars(this.value)" />
So if you were to input 'hello' into this currently, it would replace it to 'xxxxx'. I tried putting your regex into it, but it ended up replacing the square brackets instead. Basically, I need it to make whatever that is inside the square brackets stay intact when it replaces.
|
|
|
|
08-11-2011, 03:31 PM
|
Re: JavaScript Regex Question
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
I'm no regex expert myself, but I think you could use a replace with a callback function. So, first use my piece of code to find any sub string that is not in brackets, that part is sent to your callback function where you replace letters with x and numbers with 1.
Something like this (not tested):
Code:
function replace_chars(val) {
return val.replace(/[^\[.+\]]/, your_callback_function);
}
function your_callback_function(match) {
return match.replace(/[a-zA-Z]/g,'x').replace(/[0-9]/g,'1');
}
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
08-11-2011, 03:34 PM
|
Re: JavaScript Regex Question
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Also, I dont know about javascript, but php's *_replace functions can be called with array parameters instead of calling them twice. Perhaps that is also possible in javascript(?).
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
08-11-2011, 04:46 PM
|
Re: JavaScript Regex Question
|
Posts: 919
Name: Scott Kaye
Location: Ontario
|
I'm sorry, lizciz, but that didn't work  .
I'm determined to get this working. Fortunately I know PHP a little better than JS, so I wrote what I want to do in PHP:
PHP Code:
$str="[no] yes"; $parts=array_filter(preg_split('//',$str)); $first=strpos($str,"[")+1; $last=strpos($str,"]")+1; for($p=0;$p<=count($parts);$p++){ if ($p>$first&&$p<$last){$output.=$parts[$p];} else { if(ctype_alnum($parts[$p])){$output.='x';} elseif(ctype_digit($parts[$p])){$output.='1';} else{$output.=$parts[$p];} } } echo $output;
Is there a way to translate that to JS? I know about php.js, but I'd like to use JS-only functions. If there's no way to do it, I'll just stick an AJAX call to do it for me xD
Last edited by Physicsguy; 08-11-2011 at 04:47 PM..
|
|
|
|
08-11-2011, 05:59 PM
|
Re: JavaScript Regex Question
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Well, if you're gonna do it without regex any way, perhaps this will work?
Again, not tested
Code:
function replace_chars(val) {
for (i = 0; i < val.length; i++) {
if (val.charAt(i) == '[') {
while (val.charAt(++i) != ']');
} elseif (val.charAt(i) == parseInt(val.charAt(i))) {
setCharAt(val, i, '1');
} else {
setCharAt(val, i, 'x');
}
}
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
Oh, and the setCharAt() function is not my code, I copied it from here.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
Last edited by lizciz; 08-11-2011 at 06:02 PM..
|
|
|
|
08-11-2011, 06:36 PM
|
Re: JavaScript Regex Question
|
Posts: 919
Name: Scott Kaye
Location: Ontario
|
Thanks lizciz! Unfortunately, that doens't seem to work, either (I changed elseif to else if, too). It returns 'undefined'.
I'll whip up an AJAX query, and that'll be the end of my problem  Thanks for your help, though!
|
|
|
|
08-11-2011, 07:08 PM
|
Re: JavaScript Regex Question
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Well, i forgot to actually return anything 
You can try to add "return val", or stick with your ajax option 
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
08-11-2011, 10:01 PM
|
Re: JavaScript Regex Question
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Are the chars you want to keep between the [ and ] numeric only or alphanumeric?
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
08-11-2011, 11:28 PM
|
Re: JavaScript Regex Question
|
Posts: 919
Name: Scott Kaye
Location: Ontario
|
They are alphanumeric (a-zA-Z 0-9)
|
|
|
|
08-12-2011, 02:06 AM
|
Re: JavaScript Regex Question
|
Posts: 807
Name: Mattias Nordahl
Location: Sweden
|
Just for the record, I've now fixed my previous errors and actually tested it
Code:
function replace_chars(val) {
for (i = 0; i < val.length; i++) {
if (val.charAt(i) == '[') {
while (val.charAt(++i) != ']');
} else if (val.charAt(i) == parseInt(val.charAt(i))) {
val = setCharAt(val, i, '1');
} else {
val = setCharAt(val, i, 'x');
}
}
return val;
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substr(0,index) + chr + str.substr(index+1);
}
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
|
|
|
|
08-12-2011, 08:54 AM
|
Re: JavaScript Regex Question
|
Posts: 919
Name: Scott Kaye
Location: Ontario
|
It works!!!! Fantastic! Thank you lizciz 
|
|
|
|
|
« Reply to JavaScript Regex Question
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|