Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
JavaScript Regex Question
Old 08-11-2011, 11:37 AM JavaScript Regex Question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
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
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-11-2011, 11:59 AM Re: JavaScript Regex Question
Extreme Talker

Posts: 246
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
stbuchok is offline
Reply With Quote
View Public Profile
 
Old 08-11-2011, 12:59 PM Re: JavaScript Regex Question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
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)
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-11-2011, 01:56 PM Re: JavaScript Regex Question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-11-2011, 03:02 PM Re: JavaScript Regex Question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
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.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-11-2011, 03:31 PM Re: JavaScript Regex Question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-11-2011, 03:34 PM Re: JavaScript Regex Question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-11-2011, 04:46 PM Re: JavaScript Regex Question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
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
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 08-11-2011 at 04:47 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-11-2011, 05:59 PM Re: JavaScript Regex Question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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..
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-11-2011, 06:36 PM Re: JavaScript Regex Question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
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!
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-11-2011, 07:08 PM Re: JavaScript Regex Question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-11-2011, 10:01 PM Re: JavaScript Regex Question
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-11-2011, 11:28 PM Re: JavaScript Regex Question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
They are alphanumeric (a-zA-Z 0-9)
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 08-12-2011, 02:06 AM Re: JavaScript Regex Question
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
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.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-12-2011, 08:54 AM Re: JavaScript Regex Question
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
It works!!!! Fantastic! Thank you lizciz
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to JavaScript Regex Question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46324 seconds with 12 queries