Matthew
Matthew

Reputation: 2246

Replace letters with Words using JavaScript Regex

I'm making something that requires a phonetic expression. The goal is to take a random string — that has numbers, letters, and symbols — and replace the letters with words. I ran into a problem.

It doesn't want to change the first and last letters to words. This is because the expression, /\sa\s/g (for example) searches for all instances of lower-case a's that have a space in the front and back of the letter.

Let's say you have a random string h_OX%o2At

The below code returns h _ OSCAR XRAY % oscar 2 ALPHA t, but I want it to return hotel _ OSCAR XRAY % oscar 2 ALPHA tango.

Here's the code:

phoneticExp.text(password.split("").join(" ").replace(/\sa\s/g, " alpha ").replace(/\sA\s/g, " ALPHA ").replace(/\sb\s/g, " bravo ").replace(/\sB\s/g, " BRAVO ").replace(/\sc\s/g, " charlie ").replace(/\sC\s/g, " CHARLIE ").replace(/\sd\s/g, " delta ").replace(/\sD\s/g, " DELTA ").replace(/\se\s/g, " echo ").replace(/\sE\s/g, " ECHO ").replace(/\sf\s/g, " foxtrot ").replace(/\sF\s/g, " FOXTROT ").replace(/\sg\s/g, " golf ").replace(/\sG\s/g, " GOLF ").replace(/\sh\s/g, " hotel ").replace(/\sH\s/g, " HOTEL ").replace(/\si\s/g, " india ").replace(/\sI\s/g, " INDIA ").replace(/\sj\s/g, " juliet ").replace(/\sJ\s/g, " JULIET ").replace(/\sk\s/g, " kilo ").replace(/\sK\s/g, " KILO ").replace(/\sl\s/g, " lima ").replace(/\sL\s/g, " LIMA ").replace(/\sm\s/g, " mike ").replace(/\sM\s/g, " MIKE ").replace(/\sn\s/g, " november ").replace(/\sN\s/g, " NOVEMBER ").replace(/\so\s/g, " oscar ").replace(/\sO\s/g, " OSCAR ").replace(/\sp\s/g, " papa ").replace(/\sP\s/g, " PAPA ").replace(/\sq\s/g, " quebec ").replace(/\sQ\s/g, " QUEBEC ").replace(/\sr\s/g, " romeo ").replace(/\sR\s/g, " ROMEO ").replace(/\ss\s/g, " sierra ").replace(/\sS\s/g, " SIERRA ").replace(/\st\s/g, " tango ").replace(/\sT\s/g, " TANGO ").replace(/\su\s/g, " uniform ").replace(/\sU\s/g, " UNIFORM ").replace(/\sv\s/g, " victor ").replace(/\sV\s/g, " VICTOR ").replace(/\sw\s/g, " wiskey ").replace(/\sW\s/g, " WISKEY ").replace(/\sx\s/g, " xray ").replace(/\sX\s/g, " XRAY ").replace(/\sy\s/g, " yankee ").replace(/\sz\s/g, " zulu ").replace(/\sZ\s/g, " ZULU "));

I then tried to do /(\s|\s?)a(\s|\s?)/g which should of searched for the lower-case a that either had a space or an optional space in front and behind of it. That didn't work.

I'm at a loss. Here's the Fiddle

Upvotes: 1

Views: 1335

Answers (3)

Xotic750
Xotic750

Reputation: 23472

Why not avoid a regex and do something like this instead.

Javascript

var lookup = {
    a: 'alpha',
    b: 'bravo',
    c: 'charlie',
    d: 'delta',
    e: 'echo',
    f: 'foxtrot',
    g: 'golf',
    h: 'hotel',
    i: 'india',
    j: 'juliet',
    k: 'kilo',
    l: 'lima',
    m: 'mike',
    n: 'november',
    o: 'oscar',
    p: 'papa',
    q: 'quebec',
    r: 'romeo',
    s: 'siera',
    t: 'tango',
    u: 'uniform',
    v: 'victor',
    w: 'wiskey',
    x: 'xray',
    y: 'yankee',
    z: 'zulu',
};

function toPhonetic(text) {
    return text.split('').map(function (character) {
        var lower = character.toLowerCase(),
            word;

        if (lookup.hasOwnProperty(lower)) {
            word = lookup[lower];
            if (character === lower) {
                return word;
            }

            return word.toUpperCase();

        }

        return character;
    }).join(' ');
}

var password = $("#password").text(),
    phoneticExp = $("#phonetic");

phoneticExp.text(toPhonetic(password));

On jsFiddle

Upvotes: 0

Anthony Chu
Anthony Chu

Reputation: 37520

Instead of looking for the letter surrounded by whitespace, try to look for a word boundary instead. For example...

.replace(/\bh\b/g, "hotel")

Fiddle

Upvotes: 2

sonnym
sonnym

Reputation: 76

You need to also search for the anchors at the beginning and end of the string. For each character, you need to do something like the following: /(\s|^)a(\a|$)/g. I would probably also make a hash of starting/ending characters and loop over that so you do not need to make these changes in fifty-some places!

I made the pertinent changes to h and t here, for example: http://jsfiddle.net/HGrV8/

Upvotes: 1

Related Questions