Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7802155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:10:34+00:00 2026-06-02T01:10:34+00:00

After seeing Google’s april fools joke of the morse code gmail, I thought I’d

  • 0

After seeing Google’s april fools joke of the morse code gmail, I thought I’d try to create a real-time morse code converter in javascript.

I’m using regex and replace to change the morse code into character. For example:

.replace(/.- /g, "a").replace(/.-. /g, "r")

The issue I’m having is that when I’m typing .-. for “r” it give me an “a” because it sees .- first.
How can I make it replace only exact matches?

Updated and working!! Thanks to every one that helped me

http://jsfiddle.net/EnigmaMaster/sPDHL/32/ – My Original code

http://jsfiddle.net/EnigmaMaster/LDKKE/6/ – Rewritten by Shawn Chin

http://jsfiddle.net/EnigmaMaster/y9A4Y/2/ – Rewritten by Matthias Tylkowski

If anyone has other ways of writting this program please post a JsFiddle

Id love to see how else this can be done

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:10:37+00:00Added an answer on June 2, 2026 at 1:10 am

    The other answers have already covered the reasons why your example was not working so I’ll refrain from repeating them.

    However, may I suggest that since you’re already using spaces to delimit each code, a straight-forward solution would be to do a simple .split() to segment the input text into individual units then simply do a one-to-one mapping of code to chars.
    This will be a lot more efficient than repeated regex replacements and less prone to errors.

    For example:

    var morse = {  // use object as a map
        '.-': 'a', 
        '-...': 'b', 
        '-.-.': 'c', 
        // .... the rest ...
    };
    
    function translate_morse(code) {  // given code, return matching char
        return (typeof morse[code] === "undefined") ? "" : morse[code];
        // if the var is not found, the code is unknown/invalid. Here we 
        // simply ignore it but you could print out the code verbatim and use
        // different font styles to indicate an erroneous code
    }
    
    // example usage
    translated = code_text.split(" ").map(translate_morse).join("");
    

    Here’s a working example: http://jsfiddle.net/KGVAm/1/

    p.s. I’ve taken the liberty of tweaking the code and the behaviour a little, i.e. disabling the input of other chars but allowing backscape to allow corrections.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a long time I have been working with ReSharper. But after seeing CodeRush
After following the overview found here: https://developers.google.com/analytics/devguides/collection/ios/v2/ I am seeing an Unused variable tracker
I investigated GetAsyncKeyState after seeing it in a code example on how to make
After seeing several forums and tutorials for Google maps for Android, and I have
After seeing tons of web site using modal window with a nice expansion animation
I'm seeking further clarification after seeing What is responsible for releasing NSWindowController objects? I'm
I'm working on an editor for novel authors . After seeing programs like DarkRoom
After looking at several questions/answers here, I'm not seeing what I think I need.
I'm seeing this exception message coming from XslCompiledTransform.Transform(), but after handling the exception the
I'm seeing some weird stuff happening in my app. After coming back to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.