Javascript real-time morse code converter

After looking at the Google april gool joke from morse gmail code, I thought I'd try to create a real-time morse code converter in javascript.

I use regex and replace to change morse code to character. For example:

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

The problem I am facing is that when I type .-. for "r", he gives me an "a" because he first sees .- . How can I make it replace only exact matches?

Updated and working! Thanks to everyone who helped me

http://jsfiddle.net/EnigmaMaster/sPDHL/32/ - My source code

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

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

If anyone has other ways to write this program, write JsFiddle

I like to see how this can be done.

+7
source share
9 answers

Other answers already describe the reasons why your example did not work, so I will not repeat them.

However, can I assume that since you are already using spaces to delimit each code, the straightforward solution is to make a simple .split() to segment the input text into separate units, and then just do one - one mapping of the code into characters . This will be much more efficient than replacing regular expressions and less error prone.

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 is a working example: http://jsfiddle.net/KGVAm/1/

ps I allowed a little change in the code and behavior, i.e. Disable input of other characters, but allowing backscape to allow corrections.

+1
source

An interesting fact is emphasized in your question: Morse code is not a prefix code . So you need some kind of delimiters, spaces, commas, etc., in order to distinguish between code characters.

You seem to decide to use spaces, that's fine.

Saying this, what is missing in your regular expression is escaping dot characters (use /\.- / instead of /.- / )

+3
source

Another way to do this is to use a dichotomous search.

enter image description here

Graphical representation of the dichotomous lookup table: custom branches on the left of each point and on the right of each dash until the character is complete.

+3
source

If you process one Morse character at a time, start with the longest sequences, down to shorter ones.

+1
source

You must prioritize your rules and apply a replacement for longer phrases first:

 .replace(/\.-\./g,'r')// longer .replace(/\.-/g,'a')// shorter 

I suggest you make a dictionary and sort it by the length of the template, and then replace the line:

 var str = '.-.-.'; var dictionary = [ ['a', '\.-'], ['r', '\.-\.'] // ... and so on ]; dictionary .sort(function(a,b){ return a[1] > b[1]; }) .forEach(function(el,i){ str = str.replace(new RegExp(el[1],'g'),el[0]); }); 

It looks like there are no brackets, I do not seem to have found them on my phone.

update : I avoided the points used in regular expressions - I skipped this first. update2 : brackets added :) You can use unshielded dots in the dictionary and avoid them on the fly:

  var dictionary = [ ['a', '.-'], ['r', '.-.'] ]; dictionary .sort(function(a,b){ return a[1] > b[1]; }) .forEach(function(el,i){ str = str.replace(new RegExp(el[1].replace(/([^\]\.)/g,'$1\.'),'g'),el[0]); }); 
+1
source

You need to get away from the "." characters:

 /\.- / 

instead

 /.- / 

in other words. In the regular expression "." matches any character.

+1
source

In regular expressions, the dot matches any character, and it seems that you are using it without the correct image:

 /./ // this will match any character /\./ // this will match a dot (".") character 
+1
source

I think the trick here is to split the string that someone puts into pieces. After each sequence "." There must be a space. and "-". Then you can run this array and replace each entry with the corresponding letter. I would use a switch. At the end, you join the array together as a string and display it.

0
source

For those who are looking for the whole translation table:

Here is a demo , and here is the whole source code .

 var morseCode = { '.-': 'a', '-...': 'b', '-.-.': 'c', '-..': 'd', '.': 'e', '..-.': 'f', '--.': 'g', '....': 'h', '..': 'i', '.---': 'j', '-.-': 'k', '.-..': 'l', '--': 'm', '-.': 'n', '---': 'o', '.--.': 'p', '--.-': 'q', '.-.': 'r', '...': 's', '-': 't', '..-': 'u', '...-': 'v', '.--': 'w', '-..-': 'x', '-.--': 'y', '--..': 'z', '-----': '0', '.----': '1', '..---': '2', '...--': '3', '....-': '4', '.....': '5', '-....': '6', '--...': '7', '---..': '8', '----.': '9', '.--.-': 'Γ ', '.--.-': 'Γ₯', '.-.-': 'Γ€', '----': 'ch', '.-..-': 'Γ¨', '..-..': 'Γ©', '---.': 'ΓΆ', '..--': 'ΓΌ', '...--..': 'ß', '--.--': 'Γ±', '.-.-.-': '.', '--..--': ',', '---...': ':', '-.-.-.': ';', '..--..': '?', '-....-': '-', '..--.-': '_', '-.--.': '(', '-.--.-': ')', '.----.': '\'', '-...-': '=', '.-.-.': '+', '-..-.': '/', '.--.-.': '@' }; 
0
source

All Articles