Regular Expressions in HTML - javascript

I hope you are well. So, as you can see, from "a-z" I have made a code to transcript every common letter (from "a" to "z") in some symbols. How can I do it when I want to transcript the letters "th" together in different symbol? I do not want the app to translate the letters "t" and "h" separately BUT together! How can I do that? Thank you so much!!!
var theInput = txtBr.value.toLowerCase();
for (var i = 0; i < theInput.length; i++)
{
var letter = theInput.charAt( i );
if( letter.match(/[a-z\s]/i) ) {
var symbol = map[ letter ];
txtarea.innerHTML += symbol;
}

string.search() will return the position of the substring you are searching for.
let string = "nspoiuthpiifs";
let position = string.search("th"); // will return 6
You could then split the string at that position with string.split(), replace the the "th" with the symbol you desire, then rejoin the string with array.join()
Note: string.search() will return only the position of the first one it finds, so you may want to repeat this method until there are none left.

Related

Select string before some letters

If I have
var x = "3558&Hello world!&538345";
Now for selecting after I can use:
var y = x.split("&");
But I want to know can I select it before some char?
I need to get var z = 3558...
I don't want to select letters and nums from 0 to 4 because I don't know what is the length of numbers.... So is there any way to say select all before & ?
You're using the split functionality, so just grab the value at position 0 of the resulting array, that will be the substring up until the first &.
var y = x.split ("&")[0];
try this:
var y = x.split('&')[0]; // y = "3558"
You can use String.prototype.replace() with RegExp /&.+/ to match "&" and characters that follow, replace match with empty string
var z = "3558&Hello world!&538345".replace(/&.+/, "")
Lots of options! :D
function bySplit (textStr, delimiterStr) {
return textStr.split(delimiterStr)[0]
}
function byRegexReplace (textStr, delimiterStr) {
return textStr.replace(new RegExp(delimiterStr+'.*'), '')
}
function byRegexExec (textStr, delimiterStr) {
return (new RegExp('^(.*)'+delimiterStr)).exec(textStr)[1]
}
You could also write for or while loop to add chars to a result unless they encounter a matching delimiterStr, but they get messy compared to these three.

how to retrieve a string between to same charecter

I know how to use substring() but here I have a problem, I'd like to retrieve a number between two "_" from a unknown string length. here is my string for example.
7_28_li
and I want to get the 28. How can I proceed to do so ?
Thanks.
Regex
'7_28_li'.match(/_(\d+)_/)[1]
The slashes inside match make it's contents regex.
_s are taken literally
( and ) are for retrieving the contents (the target number) later
\d is a digit character
+ is "one or more".
The [1] on the end is accesses what got matched from the first set of parens, the one or more (+) digits (\d).
Loop
var str = '7_28_li';
var state = 0; //How many underscores have gone by
var num = '';
for (var i = 0; i < str.length; i++) {
if (str[i] == '_') state++;
else if (state == 1) num += str[i];
};
num = parseInt(num);
Probably more efficient, but kind of long and ugly.
Split
'7_28_li'.split('_')[1]
Split it into an array, then get the second element.
IndexOf
var str = "7_28_li";
var num = str.substring(str.indexOf('_') + 1, str.indexOf('_', 2));
Get the start and end point. Uses the little-known second parameter of indexOf. This works better than lastIndexOf because it is guaranteed to give the first number between _s, even when there are more than 2 underscores.
First find the index of _, and then find the next position of _. Then get the substring between them.
var data = "7_28_li";
var idx = data.indexOf("_");
console.log(data.substring(idx + 1, data.indexOf("_", idx + 1)));
# 28
You can understand that better, like this
var data = "7_28_li";
var first = data.indexOf("_");
var next = data.indexOf("_", first + 1);
console.log(data.substring(first + 1, next));
# 28
Note: The second argument to indexOf is to specify where to start looking from.
Probably the easiest way to do it is to call split on your string, with your delimiter ("_" in this case) as the argument. It'll return an array with 7, 28, and li as elements, so you can select the middle one.
"7_28_li".split("_")[1]
This will work if it'll always be 3 elements. If it's more, divide the length property by 2 and floor it to get the right element.
var splitstring = "7_28_li".split("_")
console.log(splitstring[Math.floor(splitstring.length/2)]);
I'm not sure how you want to handle even length strings, but all you have to do is set up an if statement and then do whatever you want.
If you know there would be 2 underscore, you can use this
var str = "7_28_li";
var res = str.substring(str.indexOf("_") +1, str.lastIndexOf("_"));
If you want to find the string between first 2 underscores
var str = "7_28_li";
var firstIndex = str.indexOf("_");
var secondIndex = str.indexOf("_", firstIndex+1);
var res = str.substring(firstIndex+1, secondIndex);

Find all matches in a concatenated string of same-length words?

I have a long Javascript string with letters like :
"aapaalaakaaiartaxealpyaaraa"
This string is actually a chained list of 3-letter-words : "aap","aal","aak","aai", "art", "axe","alp", "yaa" and "raa"
In reality I have many of these strings, with different word lengths, and they can be up to 2000 words long, so I need the fastest way to get all the words that start with a certain string. So when searching for all words that start with "aa" it should return :
"aap","aal","aak" and "aai"
Is there a way to do this with a regex ? It's very important that it only matches on each 3-letter word, so matches in between words should not be counted, so "aar" should not be returned, and also not "yaa" or "raa".
The simple way:
var results = [];
for (var i = 0; i < str.length; i += 3) {
if (str.substring(i, i + 2) === "aa") {
results.push(str.substring(i, i + 3));
}
}
Don’t ask whether it’s the fastest – just check whether it’s fast enough, first. :)
How about:
var str = 'aapaalaakaaiartaxealpyaaraa';
var pattern = /^aa/;
var result = str.match(/.{3}/g).filter(function(word) {
return pattern.test(word);
});
console.log(result); //=> ["aap","aal","aak","aai"]
"aapaalaakaaiartaxealpyaaraa".replace(/\w{3}|\w+/g,function(m){return m.match(/^aa/)?m+',':','}).split(',').filter(Boolean)

Validate First and Last name (not using regular expression)

I need to validate user input of first and last name in the one input box. I have to make sure they only use letters and no other characters and is in uppercase or lowercase ex.. John Smith. I can find a lot of ways to do this using regular expressions, but the task has specifically asked that no regular expressions be used.
Even if someone to point me to where I can find this information myself.
Thanks
Just check each letter to see if it's valid. So you create an array of valid characters, then make sure each character is in that array.
function validate_name(name) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
alphabet = alphabet + ' ' + alphabet.toUpperCase();
alphabet = alphabet.split(''); //turn it into an array of letters.
for (i=0; i<name.length; i++) {
if (!~alphabet.indexOf(name.charAt(i)) { //!~ just turns -1 into true
return false;
}
}
return true;
}
I have a feeling that, given the question, it is best to keep the solution simple.
Note this observations:
A string can be indexed like an array and has a length like an array. Thus, a string can be looped like an array.
Strings are lexically ordered. That is, "a" < "b" and "A" < "B" are both true.
String.toLower can translate "A" to "a"
Then we can start a basic C-style approach:
for (var i = 0; i < inp.lenth; i++) {
var ch = inp[ch];
if (ch == " ") .. // is space
if (ch >= "a" && ch <= "z") .. // lowercase English letter
..
Of course, the problem may be more than simply ensuring that the letters are all in { a-z, A-Z, space}. Consider these inputs - are they valid?
"John Doe"
"JohnDoe"
" John "

Detecting if a character is a letter

Given a set of words, I need to put them in an hash keyed on the first letter of the word.
I have words = {}, with keys A..Z and 0 for numbers and symbols.
I was doing something like
var firstLetter = name.charAt(0);
firstLetter = firstLetter.toUpperCase();
if (firstLetter < "A" || firstLetter > "Z") {
firstLetter = "0";
}
if (words[firstLetter] === undefined) {
words[firstLetter] = [];
}
words[firstLetter].push(name);
but this fails with dieresis and other chars, like in the word Ärzteversorgung.
That word is put in the "0" array, how could I put it in the "A" array?
You can use this to test if a character is likely to be a letter:
var firstLetter = name.charAt(0).toUpperCase();
if( firstLetter.toLowerCase() != firstLetter) {
// it's a letter
}
else {
// it's a symbol
}
This works because JavaScript already has a mapping for lowercase to uppercase letters (and vice versa), so if a character is unchanged by toLowerCase() then it's not in the letter table.
Try converting the character to its uppercase and lowercase and check to see if there's a difference. Only letter characters change when they are converted to their respective upper and lower case (numbers, punctuation marks, etc. don't). Below is a sample function using this concept in mind:
function isALetter(charVal)
{
if( charVal.toUpperCase() != charVal.toLowerCase() )
return true;
else
return false;
}
You could use a regular expression. Unfortunately, JavaScript does not consider international characters to be "word characters". But you can do it with the regular expression below:
var firstLetter = name.charAt(0);
firstLetter = firstLetter.toUpperCase();
if (!firstLetter.match(/^\wÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜäëïöüçÇßØøÅåÆæÞþÐð$/)) {
firstLetter = "0";
}
if (words[firstLetter] === undefined) {
words[firstLetter] = [];
}
words[firstLetter].push(name);
You can use .charCodeAt(0); to get the position in the ASCII Chart and then do some checks.
The ranges you are looking for are probably 65-90, 97-122, 128-154, 160-165 (inclusive), but double check this by viewing the ASCII Chart
Something like this
if((x>64&&x<91)||(x>96&&x<123)||(x>127&&x<155)||(x>159&&x<166))
Where x is the Char Code
This is fortunately now possible without external libraries. Straight from the docs:
let story = "It’s the Cheshire Cat: now I shall have somebody to talk to.";
// Most explicit form
story.match(/\p{General_Category=Letter}/gu);
// It is not mandatory to use the property name for General categories
story.match(/\p{Letter}/gu);

Categories

Resources