simple RegEx question

Discussion in 'Javascript' started by P2000, Dec 19, 2006.

  1. P2000

    P2000 Guest

    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!
     
    P2000, Dec 19, 2006
    #1
    1. Advertising

  2. P2000

    Jeremy Guest

    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(); })
     
    Jeremy, Dec 19, 2006
    #2
    1. Advertising

  3. P2000

    P2000 Guest

    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(); })
     
    P2000, Dec 19, 2006
    #3
  4. P2000

    Jeremy Guest

    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 <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
     
    Jeremy, Dec 19, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saad Malik
    Replies:
    5
    Views:
    400
    John C. Bollinger
    May 2, 2005
  2. John Salerno

    a simple regex question

    John Salerno, Apr 1, 2006, in forum: Python
    Replies:
    6
    Views:
    322
    Paddy
    Apr 2, 2006
  3. johnny

    Simple Python REGEX Question

    johnny, May 11, 2007, in forum: Python
    Replies:
    4
    Views:
    422
    James T. Dennis
    May 12, 2007
  4. Replies:
    3
    Views:
    822
    Reedick, Andrew
    Jul 1, 2008
  5. Sam Kong
    Replies:
    8
    Views:
    128
    Csaba Henk
    Mar 25, 2005
Loading...

Share This Page