Javascript first complete unicode character from a string [duplicate] - javascript

This question already has answers here:
Difference between codePointAt and charCodeAt
(3 answers)
Closed 2 years ago.
Getting the first character in a string is fairly straightforward.
const str = 'abc'
str[0] // 'a'
However, when javascript sees a unicode string, it will return the first byte of a multi-byte unicode character.
const strUnicode = '💖hi'
strUnicode[0] // '�'
Is it possible to return the first complete unicode character?
const strUnicode = '💖hi'
f(strUnicode) // '💖'

Issue is that symbols are 16-bit characters. So it takes 2 positions in a character array.
Idea:
Loop over string and validate if current character is a symbol or not.
If symbol, take character at i and i+1. Increment i to skip the processed character
If not, just pick one character
function getCharacters(str) {
const parts = []
for(let i = 0; i< str.length; i++) {
if (str.charCodeAt( i ) > 255) {
parts.push(str.substr(i, 2))
i++
} else {
parts.push(str[i])
}
}
return parts
}
const strUnicode = '💖hi'
console.log( getCharacters(strUnicode) )

Related

replace() function not working as intended [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 2 years ago.
the replace function isnt working as a thought it would. came across this piece of code. i know the replace function is to replace all punctuation marks so as to not count them as letters. but when i log a string including punctuations it counts them as well. trying to figure out why
const getLetterCount = (stringToTest) => {
const wordArray = stringToTest.split('');
let totalLetters = 0;
for (let word of wordArray) {
word.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "");
totalLetters += word.length;
}
console.log(totalLetters);
}
getLetterCount('boy/girl?') // returns 9 ( counting punctuation as well)
String.prototype.replace()
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.
You have to reassign the new value to the variable (word).
const getLetterCount = (stringToTest) => {
const wordArray = stringToTest.split('');
let totalLetters = 0;
for (let word of wordArray) {
word = word.replace(/[.,\/#!$%\^&\*;:{}=\-_` ~()]/g, "");
totalLetters += word.length;
}
console.log(totalLetters);
}
getLetterCount('boy/girl?') // returns 9 ( counting punctuation as well)

Confused why my regex expression isn't working? [duplicate]

This question already has answers here:
Alternation operator inside square brackets does not work
(2 answers)
What's the difference between () and [] in regular expression patterns?
(6 answers)
Closed 3 years ago.
Attempting to create a regex expression that splits a string at ',' and '\n' and then a passed in custom delimiter (which is signified by firstChar in my code).
Format for the string being passed in: {delimiter}\n{numbers}. I've used regex101 online and it seems to work on there but in my actual code it doesn't split at the custom delimiter so not sure what I'm doing wrong.
if (str.includes('\n')) {
let firstChar = str.slice(0, 1);
if (parseInt(firstChar)) {
strArr = str.split(/,|\n/) ;
} else {
strArr = str.split(/[,|\n|firstChar]/);
}
}
expect ';\n2;5' to equal 7 but my array splits into [";", "2;5"] for some reason.
Your first character isn't a number so you go to else condition directly, if you want a dynamic regex then you need to build it using RegExp
Also you don't need character class here
/[,|\n|firstChar]/
it should be
/,|\n|firstChar/
let splitter = (str) => {
if (str.includes('\n')) {
let firstChar = str.slice(0, 1);
if (parseInt(firstChar)) {
return str.split(/,|\n/);
} else {
let regex = new RegExp(`,|\\n|\\${firstChar}`, 'g') // building a dynamic regex here
return str.split(regex).filter(Boolean)
}
}
}
console.log(splitter(";\n2;5"))
console.log(splitter("*\n2*5"))

Remove "_100" or "_150" or "_num" alone from the end of string [duplicate]

This question already has answers here:
Remove trailing numbers from string js regexp
(2 answers)
Closed 3 years ago.
How would I remove _100 from the end of the string, It should be removed only at the end of the string.
For e.g
marks_old_100 should be "marks_old".
marks_100 should be "marks".
function numInString(strs) {
let newStr = ''
for (let i = 0; i < strs.length; i++) {
let noNumRegex = /\d/
let isAlphRegex = /[a-zA-Z]$/
if (isAlphRegex.test(strs[i])) {
newStr += strs[i]
}
}
return newStr
}
console.log(numInString('marks_100'))
Please check the following snippet:
const s = 'marks_old_100';
// remove any number
console.log(s.replace(/_[0-9]+$/, ''));
// remove three digit number
console.log(s.replace(/_[0-9]{3}$/, ''));
// remove _100, _150, _num
console.log(s.replace(/_(100|150|num)$/, ''));
Try:
string.replace(/_\d+$/g, "")
It makes use of regexes, and the $ matches the end of the string. .replace then replaces it with an empty string, returning the string without \d+ on the end. \d matches any digits, and + means to match more one or more.
Alternatively, if you want to match the end of a word, try:
string.replace(/_\d+\b/g, "")
which utilises \b, to match the end of a word.

How to check if a string has any letters or characters? [duplicate]

This question already has answers here:
Regex for field that allows numbers and spaces
(6 answers)
Closed 3 years ago.
I need to check if input has only numbers and spaces(no letters or characters).
Tried using .includes
var string = 'abc'
if(string.includes(A-Za)){
console.log('bad')
} else {
console.log('good')
}
Using Regex.test()
var string = 'abc'
if (/[A-Za-z]/.test(string)) {
console.log('bad')
} else {
console.log('good')
}
Just use a simple regex that matches numbers from start to end:
const bad = 'abc';
const good = 123;
const re = /^\d*$/;
const goodOrBad = str => re.test(str) ? "Good" : "Bad";
console.log(goodOrBad(bad));
console.log(goodOrBad(good));
console.log(check("abc"));
console.log(check("123"));
console.log(check("123 123"));
console.log(check("123 abc"));
function check(txt) {
return /^(\d|\s)*$/.test(txt) ? "good" : "bad";
}
Breakdown of: ^(\d|\s)*$
^: Start of string
$: End of string
\d: Match a number (Can also be written as [0-9])
\s: Match a space or any other whitespace character (if you just want space then )
\d|\s: Match number or space
(\d|\s)*: Match number or space 0-Many times (Can also be written as (\d|\s){0,})
Try this with this regex ^[0-9]*$
console.log( /^[0-9]*$/.test('45') ? "Good" : "Bad")
checking every character:
for (i = 0; i < string.length; i++) {
if (isNan(string[i]) && string[i] == ' ') {
console.log('bad')
} else {
console.log('good')
}
}

Convert a letter string to a pure letter for use in .match regex [duplicate]

This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I have an array of alphabets in the form of letter strings
alpha = ['a','b', etc..];
I'm counting up numbers of each letter in a word like so
for (j=0;j<alpha.length;j++){
num = word.match(/alpha[j]/g).length;}
Problem is that, for example, alpha[0] is 'a', not a and match regex only recognizes a .
How can I convert from 'a' to a so that match recognizes it?
To clarify
"ara".match(/a/g) returns ["a","a"] while "ara".match(/'a'/g) returns null.
You can construct a RegExp from a string with the RegExp constructor as described here.
for (j=0;j<alpha.length;j++){
var matches = word.match(new RegExp(alpha[j], "g"));
if (matches) {
num = matches.length;
// other code here to process the match
}
}
This assumes that none of the characters in the alpha array will be special characters in a regular expression because if they are, you will have to escape them so they are treated as normal characters.
As jfriend00 suggests, you can use the RegExp constructor like:
var re, num, matches;
for (j=0; j<alpha.length; j++){
re = new RegExp(alpha[j],'g');
matches = word.match(re);
num = matches? matches.length : 0;
}
Note that another way (shorter, faster, simpler) is:
var num = word.split('a').length - 1; // 'ara' -> 2

Categories