Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > simple RegEx question

Reply
Thread Tools

simple RegEx question

 
 
P2000
Guest
Posts: n/a
 
      12-19-2006
I want to make a regex for the replace function that will replace any
"a" with "A" and any "b" with "B".

This is what I did so far... (I'm trying to learn this RegEx thing)

<form>
<input name="source" onkeyup="target.value=s.value.replace(/a/g,
'A')"/><br />
<input name="target" />
</form>

can I do it on the same replace?

Thanks!

 
Reply With Quote
 
 
 
 
Jeremy
Guest
Posts: n/a
 
      12-19-2006
P2000 wrote:
> I want to make a regex for the replace function that will replace any
> "a" with "A" and any "b" with "B".
>
> This is what I did so far... (I'm trying to learn this RegEx thing)
>
> <form>
> <input name="source" onkeyup="target.value=s.value.replace(/a/g,
> 'A')"/><br />
> <input name="target" />
> </form>
>
> can I do it on the same replace?
>
> Thanks!
>


s.value.replace(/(a|b)/g, function(str) { return str.toUpperCase(); })
 
Reply With Quote
 
 
 
 
P2000
Guest
Posts: n/a
 
      12-19-2006
Thanks!

What about something more general?
Sorry for not making it clear in the first place, but my goal is to
create something to convert between different keyboard in different
languages (for people how need to use their native keyboard from a
different place).

So... how can I change it without the uppercase, say like that:
a->X
b->Y
c->Z

do I do it with an array of some sort?

Thanks again,
Sam


Jeremy wrote:
> P2000 wrote:
> > I want to make a regex for the replace function that will replace any
> > "a" with "A" and any "b" with "B".
> >
> > This is what I did so far... (I'm trying to learn this RegEx thing)
> >
> > <form>
> > <input name="source" onkeyup="target.value=s.value.replace(/a/g,
> > 'A')"/><br />
> > <input name="target" />
> > </form>
> >
> > can I do it on the same replace?
> >
> > Thanks!
> >

>
> s.value.replace(/(a|b)/g, function(str) { return str.toUpperCase(); })


 
Reply With Quote
 
Jeremy
Guest
Posts: n/a
 
      12-19-2006
P2000 wrote:
> Thanks!
>
> What about something more general?
> Sorry for not making it clear in the first place, but my goal is to
> create something to convert between different keyboard in different
> languages (for people how need to use their native keyboard from a
> different place).
>
> So... how can I change it without the uppercase, say like that:
> a->X
> b->Y
> c->Z
>
> do I do it with an array of some sort?
>
> Thanks again,
> Sam
>


You'll find the regulars here to be very much against top-posting

I think you're going to have to pull the functionality out of the
onkeyup attribute and put it in its own function:

<script type="text/javascript">

var letterMappings = {
'a': 'X',
'b': 'Y',
'c': 'Z'
};

function getMapping(char)
{
return letterMappings[char] || char;
}

function remapInput(element, target)
{
document.getElementById('output').value =
element.value.replace(/./g, getMapping);
}
</script>

....

<input name="source" type="text" onkeyup="remapInput(this);" />
<input id="output" type="text" />


Even better would be to remove the contents of that <script> to a
separate .js file - particularly for XHTML, failure to remove or escape
(the latter of which destroys readability of the code) javascript to a
separate file causes validation errors.

In the function remapInput, the regex
/./
matches any single character. The character is then passed to
getMapping, which returns either the mapped character (as defined in the
letterMappings object), or, if no mapping is defined, the original
character.


Jeremy
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Simple Python REGEX Question johnny Python 4 05-12-2007 09:38 PM
a simple regex question John Salerno Python 6 04-02-2006 02:55 PM
RegEx Woes! Please Help, Simple Question Saad Malik Java 5 05-02-2005 04:06 PM
(Maybe) a simple question about regex Sam Kong Ruby 8 03-25-2005 01:25 PM



Advertisments