I want to write a program in JS where I can make the text typed by user into short form.
for example:
and - &
are - r
see you - c u
I have used the str = str.replace(/ and /g, " & "); method till now but this method wont work for the same text typed in different case.
eg.
And, AND, anD wont be converted.
How can I convert those?
instead of : str = str.replace(/ and /g, " & ");
use: str = str.toLowerCase().replace(/ and /g, " & ");
and theres also "str.toUpperCase();"
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
www.amrosama.com | the unholy methods of javascriptCode:eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
Force the entire string into lowercase, use split to make it into an array.
Then iterate through the array:
That is a poor way of doing it though.Code:if (arn[i] == "and") { document.write("&"); }
I would use a map structure where the key is what you want to replace, and the value is what you are replacing it with.
Example:
Then you can force the string into lowercase, and iterate through the array. Checking if the word is in the map and if it is make the change.Code:map.put("and","&"); map.put("are","r");
I'm not sure if there is a map object in javascript, but that is a quick and easy way to do it.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks