473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

simple RegEx question

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!

Dec 19 '06 #1
3 1214
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(); })
Dec 19 '06 #2
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(); })
Dec 19 '06 #3
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 :-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 <scriptto 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
Dec 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Howard Dean | last post by:
I have a string with several carriage returns in it. For example: "This is my test string" I wish to convert it to "This is my test string" (remove all carriage returns. Can someone tell me...
2
by: K. Shier | last post by:
my regex for matching phone #'s: \({0,1}(?<areacode>{3})\){0,1}( |-){0,1}(?<prefix>{3})( |-){0,1}(?<suffix>{4}) which i then .Replace to become: (${areacode}) ${prefix}-${suffix} question: ...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
20
by: Larry Woods | last post by:
I'm drawing a blank... What is the regular expression for search a string for NO occurances of a substring? Example: I want to find all lines that do NOT have the substing "image" in them. ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
2
by: Warren | last post by:
Sorry to ask something so elementary, but I'm just not getting it. suppose I want to match the value of parameter b from a URL like http://www.abcd.com/?a=1&b=2&c=3 I can use this regex...
4
by: bg_ie | last post by:
I have the following Regex - oldString = @"C:\ta"; Regex regexEndsInBackslash = new Regex(oldString + @"\\", RegexOptions.Singleline | RegexOptions.IgnoreCase); Any ideas why none of the...
4
by: MaxMax | last post by:
I want to add a text to another text using regular expressions... An example before= "foo" after= "foobar" before= "one" after= "onebar" before = "" after = "bar"
2
by: Sin Jeong-hun | last post by:
I'm trying to replace \n with @(representing the actual new line character). Of course, \\n should not be replaced. What I'm trying to do is simply what the good-old printf does. For example, "\\n...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.