How can i get the number and their unit? - javascript

i want to create a regex to "- any number + unit" in a string
for example i have this string:
hello- world- 86 lo. => 86 lo
in the -world- 6 lb => 6 lb
and- the- dog -8kl => 8kl
let data='in the world- 6 lo'
let reg = /-[0-9][a-z]/gi;
let matches = data.match(reg);
console.log(matches)
with his answer:
let data='in the world- 6 lo'
let reg = /-\s*([0-9]+\s*[a-z]+)/;
let matches = data.match(reg);
console.log(matches)
i get two answer
[
"- 6 lo",
"6 lo"
]
i want to get only the second => "6 lo"

Match the hyphen and 0+ whitespace chars. The capture in group 1 matching 1+ digits, optional whitespace chars and 1 or more chars a-z.
A specific match with an optional decimal part, adding the units:
-\s*([0-9]+(?:\.\d+)?(?:\s*(?:l[ob]|kl))?)\b
Regex demo
const regex = /-\s*([0-9]+(?:\.\d+)?(?:\s*(?:l[ob]|kl))?)\b/g;
const str = `hello- world- 86 lo
in the -world- 6 lb
and- the- dog -8kl
hello- world- 86.6 lo
hello- world- 86`;
while ((m = regex.exec(str)) !== null) {
console.log(m[1]);
}
Or a more broad match:
-\s*([0-9]+(?:\.\d+)?(?:\s*[a-z]+)?)\b
Regex demo
const regex = /-\s*([0-9]+(?:\.\d+)?(?:\s*[a-z]+)?)\b/g;
const str = `hello- world- 86 lo
in the -world- 6 lb
and- the- dog -8kl
hello- world- 86.6 lo
hello- world- 86`;
let m;
while ((m = regex.exec(str)) !== null) {
console.log(m[1]);
}

let data='in the world- 6 lo'
let reg = /\d+\s?[A-Za-z]{2}/gi;
let matches = data.match(reg);
console.log(matches)
try this, you can replace [A-Za-z] with the only units you need.

Well your data is a little odd, especially in the last case where it seems like the value might be negative. However, this regex should work for the cases you describe, and you can recombine the parts (sign, value, and unit) in whatever way you want afterward.
let data1 = 'in the world- 6 lo'
let data2 = 'in the -world- 6 lb'
let data3 = 'and- the- dog -8kl'
let reg = /- ?(-|\+)?(\d+\.?\d*) ?(\w+)/;
[data1, data2, data3].forEach(s => {
console.log("Checking string:", s);
const matches = s.match(reg);
console.log("Sign:", matches[1] ?? "+");
console.log("Value:", matches[2]);
console.log("Unit:", matches[3]);
});
Note that I'd avoid using the global flag g since it prevents the .match method from returning the contents of each capture group.

Make a match against the regexp and then map the result-array of this to get rid of the "- " at the beginning and return it.
In the regexp the minus had to be masked with , the spaces are optional, the unit accept all letters. If there are e.g. only max 2 letters wished than add {1,2}.
Because you wanted all results in the string (the g-parameter) I can't use the brackets in the regexp. So I had to map over the results and use here another regexp to get the desired part of the results.
function test(str) {
let reg = /\- ?[0-9]+ ?[a-z]+/gi;
let result = str.match(reg);
return result.map(res => {
return res.match(/\- ?(.*)/)[1];
});
}
console.log(test('hello- world- 86 lo, test-912 kg.'));
console.log(test('in the -world- 6 lb '));
console.log(test('and- the- dog -8kl'));

Related

how to split string every 2 character

var alphabet = "FIN SLHOJVHEN GYKOHU";
I want to split it every 2 character so it would print
"I LOVE YOU "
I already try this but it didn't work
for (var i = 0 ; i \< alphabet.length ; i+=2 ){
alphabet.split(i)
correct me please
You can transform the string into an array, filter it and make it a string again.
let alphabet = "FIN SLHOJVHEN GYKOHU";
alphabet = [...alphabet].filter((_, i) => i%2).join("");
console.log(alphabet); //I LOVE YOU;
Also this one results in I LOVE YOU (regex101 demo).
// the string
let alphabet = 'FIN SLHOJVHEN GYKOHU';
// capture ^ start or chr, match latter chr
alphabet = alphabet.replace(/(^|.)./g, '$1');
console.log(alphabet);
Since the split function will split the given string by the delimiter you passed, it seems to me that you wish to first split the words (using empty space) contained in the encoded string and only then take only the characters at even positions to include in the decoded string.
This is a demo achieving that:
const encoded = "FIN SLHOJVHEN GYKOHU";
const words = encoded.split(' ');
let decoded = '';
words.forEach((word)=>{
for (let i=1;i<word.length;i+=2){
decoded += word[i];
}
decoded += ' ';
});
console.log(decoded);
Using a regex replacement approach we can try:
var alphabet = "FIN SLHOJVHEN GYKOHU";
var output = alphabet.replace(/[A-Z]([A-Z]|(?=\s))/g, "$1");
console.log(output);
Here is an explanation of the regex pattern:
[A-Z] match a single (odd) uppercase letter
( open capture
[A-Z] an uppercase letter
| OR
(?=\s) lookahead and find a space
)
In other words, we match an odd letter and then capture the next letter, unless that odd letter happen to be the last in the word. Then we replace with just the captured even letter, if available.
you already have different ways to do it, I'm adding one, so you'll get totally confused! hehe
This one is recursive:
we take your string alphabet
first 2 letters (every 2)
last one of this 2 characters string is stored in toPrint variable
delete the first 2 characters from alphabet
... loop till alphabet empty
Your toPrint has I Love You
Certainly not the fastest one, but nice.
let alphabet = "FIN SLHOJVHEN GYKOHU";
let toPrint = '';
do {
let temp = alphabet.slice(0, 2);
toPrint += temp[1];
alphabet = alphabet.slice(2, alphabet.length);
} while (alphabet !== '');
console.log(toPrint);
You can start your loop at 1 as you want every second character, and note that \< should be <
In the loop, i is the position of the character, so you would still have to get the character for that position and then assemble the resulting string.
var alphabet = "FIN SLHOJVHEN GYKOHU";
var result = "";
for (var i = 1; i < alphabet.length; i += 2) {
result += alphabet[i];
}
console.log(result);
If you want to take the spaces into account and only get the 2nd non whitespace character you might:
get the separate words by splitting on whitespace chars
remove the empty entries
join every second character to a string
join the parts from the initial split with a space
const alphabet = " FIN SLHOJVHEN GYKOHU ";
const result = alphabet
.split(/\s+/)
.filter(Boolean)
.map(s => s.split("").filter((s, i) => i % 2).join(""))
.join(" ");
console.log(result);
If you have a browser where a positive lookbehind for a regex is supported:
const alphabet = " FIN SLHOJVHEN GYKOHU ";
const result = alphabet
.split(/\s+/)
.filter(Boolean)
.map(s => s.match(/(?<=^(?:..)*.)./g).join(""))
.join(" ");
console.log(result);
var alphabet = "FIN SLHOJVHEN GYKOHU";
const arrayAlpha = alphabet.split('');
let stringToPrint = '';
for(let i=1; i<arrayAlpha.length; i+=2){
stringToPrint = stringToPrint + arrayAlpha[i]
}
console.log(stringToPrint)

camelCase to kebab-case

I have a kebabize function which converts camelCase to kebab-case. I am sharing my code. Can it be more optimized? I know this problem can be solved using regex. But, I want to do it without using regex.
const kebabize = str => {
let subs = []
let char = ''
let j = 0
for( let i = 0; i < str.length; i++ ) {
char = str[i]
if(str[i] === char.toUpperCase()) {
subs.push(str.slice(j, i))
j = i
}
if(i == str.length - 1) {
subs.push(str.slice(j, str.length))
}
}
return subs.map(el => (el.charAt(0).toLowerCase() + el.substr(1, el.length))).join('-')
}
kebabize('myNameIsStack')
const kebabize = str => {
return str.split('').map((letter, idx) => {
return letter.toUpperCase() === letter
? `${idx !== 0 ? '-' : ''}${letter.toLowerCase()}`
: letter;
}).join('');
}
console.log(kebabize('myNameIsStack'));
console.log(kebabize('MyNameIsStack'));
You can just check every letter is if upperCase or not and replace it.
I have a one-liner similar to Marc's but with a simpler Regular Expression and ~20% faster according my benchmark (Chrome 89).
const kebabize = (str) => str.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? "-" : "") + $.toLowerCase())
const words = ['StackOverflow', 'camelCase', 'alllowercase', 'ALLCAPITALLETTERS', 'CustomXMLParser', 'APIFinder', 'JSONResponseData', 'Person20Address', 'UserAPI20Endpoint'];
console.log(words.map(kebabize));
[A-Z]+(?![a-z]) matches any consecutive capital letters, excluding any capitals followed by a lowercase (signifying the next word). Adding |[A-Z] then includes any single capital letters. It must be after the consecutive capital expression, otherwise the expression will match all capital letters individually and never match consecutives.
String.prototype.replace can take a replacer function. Here, it returns the lowercased matched capital(s) for each word, after prefixing a hyphen when the match offset is truthy (not zero - not the first character of the string).
I suspect Marc's solution is less performant than mine because by using replace to insert hyphens and lowercasing the whole string afterwards, it must iterate over the string more than once, and its expression also has more complex look aheads/behind constructs.
Benchmark
RegEx is faster!
Unlike what you might think, the RegEx way of doing this is actually significantly faster! See benchmark.
The function below supports converting both camelCase and PascalCase into kebab-case:
function toKebabCase(str) {
return str.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
}
Here is my solution:
Works with camelCase and PascalCase:
let words = ['StackOverflow', 'camelCase', 'alllowercase', 'ALLCAPITALLETTERS', 'CustomXMLParser', 'APIFinder', 'JSONResponseData', 'Person20Address', 'UserAPI20Endpoint'];
let result = words.map(w => w.replace(/((?<=[a-z\d])[A-Z]|(?<=[A-Z\d])[A-Z](?=[a-z]))/g, '-$1').toLowerCase());
console.log(result);
/*
Returns:
[
"stack-overflow",
"camel-case",
"alllowercase",
"allcapitalletters",
"custom-xml-parser",
"api-finder",
"json-response-data",
"person20-address",
"user-api20-endpoint"
]
*/
Explanation:
Match any of the following regular expressions:
Find any capital letter, that is immediately preceeded by a small letter or a number, or
Find any capital letter, that is immediately preceeded by a capital letter or a number, that is immediately followed by a small letter
Replace the captured position with a dash ('-') followed by the captured capital letter
Finally, convert the whole string to lowercase.
I would use something like this.
function kebabize(string) {
// uppercase after a non-uppercase or uppercase before non-uppercase
const upper = /(?<!\p{Uppercase_Letter})\p{Uppercase_Letter}|\p{Uppercase_Letter}(?!\p{Uppercase_Letter})/gu;
return string.replace(upper, "-$&").replace(/^-/, "").toLowerCase();
}
const strings = ["myNameIsStack", "HTTPRequestData", "DataX", "Foo6HelloWorld9Bar", "Áb"];
const result = strings.map(kebabize);
console.log(result);
This snippet replaces all uppercase characters before or after a non-uppercase character with - followed by the uppercase. It then removes the - at the start of the string (if there is any) and downcases the whole string.
Simple solution for older browsers:
var str = 'someExampleString'
var i
function camelToKebab() {
var __str = arguments[0]
var __result = ''
for (i = 0; i < __str.length; i++) {
var x = __str[i]
if(x === x.toUpperCase()) {
__result += '-' + x.toLowerCase()
} else {
__result += x
}
}
return __result
}
console.log(str, '->', camelToKebab(str))
Here is the solution I came up with:
let resultDiv = document.querySelector(".result");
let camelCase = "thisIsCamelCase";
let kebabCase;
kebabCase = camelCase.split('').map(el=> {
const charCode = el.charCodeAt(0);
if(charCode>=65 && charCode<=90){
return "-" + el.toLowerCase()
}else{
return el;
}
})
return(kebabCase.join(''))

Using regex with javascript to money string transform

I have a money value as "R$ 2.200,00" where my thousand separators are . and my decimal separator is ,.
I would like um regex to transform this string into a valid MySQL decimal number.
Possible values:
"R$ 23.000,20", "23.000,30", "R$ 1300,20", "R$ 100", "161,43256"
The results would be:
"23000.20", "23000.30", "1300.20", "100", "161.43256"
My attempt is below, but it does not work. I would like to accept only numbers and dot(.) for the results.
const str = "R$ 2.200,000";
const res = str
.replace(/R$/gi, "")
.replace(/./gi, "")
.replace(/,/gi, ".");
console.log(res);
With optimisations this seems to be working:
const possible = [
"R$ 23.000,20",
"23.000,30",
"R$ 1300,20",
"R$ 100 ",
"161,43256",
"R$ -23.000,20",
"-23.000,30",
"R$ -1300,20",
"R$ -100",
"-161,43256"
]
const rx = /[^-\d,]+/g // Match everything except digits, comma, dot and negative
const parseNumber = num =>
num
.replace(rx, '') // Filter out non numeric entities
.replace(',', '.') // Replacing comma with dot
possible.forEach(num => {
console.log(num, '\t->\t', parseNumber(num))
})
this regEx replace/remove: (/[^\w\s]/gi) all special globaly signs ("$", ".", ","),
next one give the digit part (/\D/g)
and finally with string methods substring(,) place a ',' between the last two digits in the length of the string.
let input = "R$ 2.200,000";
let res = input.replace(/[^\w\s]/gi, '')
let str = res.replace(/\D/g,'');
let resStr = str.substring(0,str.length-2)+"."+str.substring(str.length-2);
console.log(resStr)
Edit: However this will not work if you have more than one thousand seperator, do not use it if you may have.
So, if you don't have to use regex, you can use replace function with strings too.
const str = "R$ 2.200,00";
const res = str
.replace('R$ ', "")
.replace('.', "")
.replace(',', ".")
console.log(res);
The code above will give you the format you want as a String. If you need Float, you can use parseFloat function.
console.log(parseFloat(res));
You can even use toFixed if you want absolute number of numbers after floating point
console.log(parseFloat(res).toFixed(5));
let input1 = "R$ 2.200,000";
let input2 = "23.000,30";
let input3 = "R$ 1300,20";
let input4 = "R$ 100";
let input5 = "161,43256";
const formatMoney = (inpStr) => {
let res = inpStr
res = res.replace(/[$R\s]/g, '');
let idx = res.indexOf('.')
res = res.replace(/,/, '.')
for (let i in res) {
if (res[i] === '.' && i !== i) {
res[i] = ','
}
}
return res
}
console.log(formatMoney(input1))
console.log(formatMoney(input2))
console.log(formatMoney(input3))
console.log(formatMoney(input4))
console.log(formatMoney(input5))
You could do that in two steps.
Step 1: remove characters
Substitute matches of the following regular expression with empty strings.
/(?:^R\$ +|[,.](?=.*[,.]))/gm
This will convert the strings
R$ 23.000,20
23.000,30
R$ 1300,20
R$ 100
161,43256
R$ 23.123.000,20
to
23000,20
23000,30
1300,20
100
161,43256
23123000,20
Javascript's regex engine performs the following steps.
(?: : begin a non-capture groups
^ : beginning of string
R\$ + : match 'R$' followed by 1+ spaces
| : or
[,.] : match ',' or '.'
(?= : begin positive lookahead
.* : match 0+ characters other than line terminators
[,.] : match ',' or '.'
) : end positive lookahead
) : end non-capture group
Step 2: convert comma, if present, to a period
Substitute matches of the following regular expression with a period.
/[,.]/gm
That would convert the above strings to the following.
23000.20
23000.30
1300.20
100
161.43256
23123000.20
Restart your engine!

JavaScript: Add space between Char and Number with Regex

Hello I have a plate number BZ8345LK and want convert to BZ 8345 LK (adding space between char and number).
I tried with this Regex but not working, only space first char with number. Ex BZ 8345LK, the 'LK' keep not space with number.
var str = 'BZ8345LK';
str.replace(/[^0-9](?=[0-9])/g, '$& ');
# return BZ 8345LK, I want BZ 8345 LK
You can use this regex
[a-z](?=\d)|\d(?=[a-z])
[a-z](?=\d) - Match any alphabet followed by digit
| - Alternation same as logical OR
\d(?=[a-z]) - Any digit followed by alphabet
let str = 'BZ8345LK'
let op = str.replace(/[a-z](?=\d)|\d(?=[a-z])/gi, '$& ')
console.log(op)
You should alternate with the other possibility, that a number is followed by a non-number:
var str = 'BZ8345LK';
console.log(str.replace(/[^0-9](?=[0-9])|[0-9](?=[^0-9])/g, '$& '));
An anoher option is to use:
^[^\d]+|[\d]{4}
Search for any not numeric character [^\d] followed by 4 numeric [\d]{4} characters
const str = 'BZ8345LK'
let answer = str.replace(/^[^\d]+|[\d]{4}/gi, '$& ')
console.log(answer)
Try with this
var str = "BZ8345LK";
var result = str.replace(/([A-Z]+)(\d+)([A-Z]+)/, "$1 $2 $3");
console.log(result);

What should be the regex to match the start of a string

I have a string something like and I want to match just the first '{' of every {{xxxx}} pattern
{{abcd}}{{efg}}{{hij}}
{{abcd}}{{efg}}{{hij}}{
I tried with /(\s|^|.){/g but this pattern matches
{{abcd}}{{efg}}{{hij}}
Can some one guide me in the right direction
You need to use /(^|[^{]){/g (that matches and captures into Group 1 start-of-string or any char other than {, and then matches a {) and check if Group 1 matched at each RegExp#exec iteration. Then, if Group 1 matched, increment the match index:
var re = /(^|[^{]){/g;
var str = "{{abcd}}{{efg}}{{hij}}\n{{abcd}}{{efg}}{{hij}}{";
// 0 8 15 23 31 38 45
var m, indices = [];
while ((m = re.exec(str)) !== null) {
indices.push(m.index + (m[1] ? 1 : 0));
}
console.log(indices);

Categories