Regular Expression in JavaScript with multiple conditions - javascript

I would like to remove in javascript from my text_string all characters that are not letters (in all languages) and numbers. I can do it individually. But how can I put both in ONE expression, so that both conditions are true at the same time?
var text_string = '!#Ab+Z1_↕.🍏2ü翻訳';
text_string = text_string.replace(/\P{Letter}/gu, '');
text_string = text_string.replace(/\P{Number}/gu, '');
text_string = text_string.replace(/[^#]/, '');
// should be replaced to #AbZ12ü翻訳

You can use this regex in unicode for search:
[^\p{Letter}\p{Number}#]+
and replace with empty string.
RegEx Demo
Code:
const regex = /[^\p{Letter}\p{Number}#]+/gu;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('[^\\p{Letter}\\p{Number}#]+', 'gu')
const str = `!#Ab+Z1_↕.🍏2ü翻訳`;
const result = str.replace(regex, '');
console.log(result);
RegEx Breakup:
[^\p{Letter}\p{Number}#]+: In a character class match any character that is not # not a unicode letter and not a unicode number.
Remember that \p{something} is inverse of \P{something}

Related

How to slice optional arguments in RegEx?

Actually i have the following RegExp expression:
/^(?:(?:\,([A-Za-z]{5}))?)+$/g
So the accepted input should be something like ,IGORA but even ,IGORA,GIANC,LOLLI is valid and i would be able to slice the string to 3 group in this case, in other the group number should be equals to the user input that pass the RegExp test.
i was trying to do something like this in JavaScript but it return only the last value
var str = ',GIANC,IGORA';
var arr = str.match(/^(?:(?:\,([A-Za-z]{5}))?)+$/).slice(1);
alert(arr);
So the output is 'IGORA' while i would it to be 'GIANC' 'IGORA'
Here is another example
/^([A-Z]{5})(?:(?:\,([A-Za-z]{2}))?)+$/g
test of regexp may have at least 5 chart string but it also can have other 5 chart string separated with a comma so from input
IGORA,CIAOA,POPOP
I would have an array of ["IGORA","CIAOA","POPOP"]
You can capture the words in a capturing surrounded by an optional preceding comma or an optional trailing comma.
You can test the regex here: ,?([A-Za-z]+),?
const pattern = /,?([A-Za-z]+),?/gm;
const str = `,IGORA,GIANC,LOLLI`;
let matches = [];
let match;
// Iterate until no match found
while ((m = pattern.exec(str))) {
// The first captured group is the match
matches.push(m[1]);
}
console.log(matches);
There are other ways to do this, but I found that one of the simple ways is by using the replace method, as it can replace all instances that match that regex.
For example:
var regex = /^(?:(?:\,([A-Za-z]{5}))?)+$/g;
var str = ',GIANC,IGORA';
var arr = [];
str.replace(regex, function(match) {
arr[arr.length] = match;
return match;
});
console.log(arr);
Also, in my code snippet you can see that there is an extra coma in each string, you can solve that by changing line 5 to arr[arr.length] = match.replace(/^,/, '').
Is this what you're looking for?
Explanation:
\b word boundary (starting or ending a word)
\w a word ([A-z])
{5} 5 characters of previous
So it matches all 5-character words but not NANANANA
var str = 'IGORA,CIAOA,POPOP,NANANANA';
var arr = str.match(/\b\w{5}\b/g);
console.log(arr); //['IGORA', 'CIAOA', 'POPOP']
If you only wish to select words separated by commas and nothing else, you can test for them like so:
(?<=,\s*|^) preceded by , with any number of trailing space, OR is the first word in list.
(?=,\s*|$) followed by , and any number of trailing spaces OR is last word in list.
In the following code, POPOP and MOMMA are rejected because they are not separated by a comma, and NANANANA fails because it is not 5 character.
var str = 'IGORA, CIAOA, POPOP MOMMA, NANANANA, MEOWI';
var arr = str.match(/(?<=,\s*|^)\b\w{5}\b(?=,\s*|$)/g);
console.log(arr); //['IGORA', 'CIAOA', 'MEOWI']
If you can't have any trailing spaces after the comma, just leave out the \s* from both (?<=,\s*|^) and (?=,\s*|$).

replacing all String in javascript using regex

i have a dynamic string expression
var expression = "count+count1+12-(count3+count4)";
I want to append v[...] in each string like this output
Output:-
v[count]+v[count1]+12-(v[count3]+v[count4]);
i have tried this regex expression,
expression = expression.replace(/[a-z]+|[A-Z]+/g, "v["/$1/"]").replace(/[\(|\|\.)]/g, "");
is it possible to write regex expression regex string.
You may use
var expression = "count+count1+12-(count3+count4)";
var res = expression.replace(/\b[a-z]\w*/ig, "v[$&]");
console.log(res);
Details:
\b - a leading word boundary
[a-z] - an ASCII letter
\w* - 0+ word chars ([a-zA-Z0-9_]).
The replacement contains $&, a backreference to the whole match.
Another solution that splits with the math operators and only wraps with v[...] those substrings that are not a number or the operator:
var expression = "count+count1+12+234.56-(count3+count4)";
var res = expression.split(/([-+\/*])/).map(function(x) {
return /^(\d*\.?\d+|[-*\/+])$/.test(x) ? x : "v["+x+"]";
}).join("");
console.log(res);

Split a string according to flanking characters in javascript

Javascript lets you split a string according to regular expression. Is it possible to use this functionality to split a string only when the delimiter is flanked by certain characters?
For example, if I want to split the string 12-93 but not at-13 using the - character? Is that possible?
Using a regular expression seems promising, but doing "12-93".split(/[0-9]-[0-9]/) yields ["1", "3"] because the flanking digits are considered to be part of the delimiter.
Can I specify the above split pattern (a dash preceded and followed by a digit) without chopping the flanking digits?
Other Examples
"55,966,575-165,162,787" should yield ["55,966,575", "165,162,787"]
"55,966,575x-165,162,787" should yield ["55,966,575x-165,162,787"]
"sdf55,966,575-165,162,787" should yield ["sdf55,966,575", "165,162,787"]
Using two adjacent character sets seems to work.
See example at https://regex101.com/r/uFHMW1/1
([0-9,a-z]+?[0-9]+)-([0-9]+[0-9,a-z]+)
Try this (live here https://repl.it/EOOQ/0 ):
var strings = [
"55,966,575-165,162,787",
"55,966,575x-165,162,787",
"sdf55,966,575-165,162,787",
];
var pattern = '^([0-9,a-z]+?[0-9]+)-([0-9]+[0-9,a-z]+)$';
var regex = new RegExp(pattern, 'i');
var matched = strings.map(function (string) {
var matches = string.match( regex );
if (matches) {
return [matches[1], matches[2]];
} else {
return [string];
}
});
console.log(matched)
You can also run the above expression as split() like:
string.split(re).filter( str => str.length )
where Array.filter() is used to get rid of the leading and trailing empty strings created when the RegExp matches your input.
var strings = [
"55,966,575-165,162,787",
"55,966,575x-165,162,787",
"sdf55,966,575-165,162,787",
];
var pattern = '^([0-9,a-z]+?[0-9]+)-([0-9]+[0-9,a-z]+)$';
var regex = new RegExp(pattern, 'i');
var matched = strings.map( string => string.split(regex).filter( str => str.length ) );
console.log(matched)
Try using a non-capturing lookahead. You are using a regex that captures all of the characters found, then uses that result as the split character(s).

JS - Split string into substrings by regex

Let's say I have a string that starts by 7878 and ends by 0d0a or 0D0A such as:
var string = "78780d0101234567890123450016efe20d0a";
var string2 = "78780d0101234567890123450016efe20d0a78780d0103588990504943870016efe20d0a";
var string 3 = "78780d0101234567890123450016efe20d0a78780d0103588990504943870016efe20d0a78780d0101234567890123450016efe20d0a"
How can I split it by regex so it becomes an array like:
['78780d0101234567890123450016efe20d0a']
['78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a']
['78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a']
You can split the string with a positive lookahead (?=7878). The regex isn't consuming any characters, so 7878 will be part of the string.
var rgx = /(?=7878)/;
console.log(string1.split(rgx));
console.log(string2.split(rgx));
console.log(string3.split(rgx));
Another option is to split on '7878' and then take all the elements except first and add '7878' to each of them. For example:
var arr = string3.split('7878').slice(1).map(function(str){
return '7878' + str;
});
That works BUT it also matches strings that do NOT end on 0d0a. How
can I only matches those ending on 0d0a OR 0D0A?
Well, then you can use String.match with a plain regex.
console.log(string3.match(/7878.*?0d0a/ig));

How to strip last element of dash delimited string in Javascript

I have string delimited with dashes like:
x#-ls-foobar-takemeoff-
How can I remove takemeoff- using javascript where takemeoff- can be any amount of characters ending in a dash?
var str = "x#-ls-foobar-takemeoff-";
var newStr = str.replace(/[^-]+-$/,"");
Basic regular expression says
[^-]+ <-- Match any characters that is not a dash
- <-- Match a dash character
$ <-- Match the end of a string
If you have a string str, you can do the following:
str = str.substr(0, str.lastIndexOf("-", str.length - 2));
Using substr() and lastIndexOf():
var myStr = "x#-ls-foobar-takemeoff-";
myStr = myStr.substr(0, myStr.length-1); // remove the trailing -
var lastDash = myStr.lastIndexOf('-'); // find the last -
myStr = myStr.substr(0, lastDash);
alert(myStr);
Outputs:
x#-ls-foobar
jsFiddle here.

Categories

Resources