simple RegEx question

P

P2000

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!
 
J

Jeremy

P2000 said:
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(); })
 
P

P2000

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
 
J

Jeremy

P2000 said:
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 :p

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top