Regex match with 3 condition? - javascript

I will creating math program .If i wish to solve i need separate to the equation using with regex.
For example:
`10x-10y2+100x-100k=100`
the format will separate with regex output: "10x" ,"100x" ,"100k" ,"10y2","100"
I have already code for that with separate match function
There are:
for num[a-z]num : ` /([\-+])?\s*(\d+)?([a-z]?(\d+))/g`
for num[a-z] : ` /([\-+])?\s*(\d+)?([a-z]?(\d+))/g`
fon num : `/\b[+-]?[\d]+\b/g`
I need all this three match function within one regex match function.some one help to combine the code with single regex expression
Note :i need match function only not a split beacause i apply that regex expression into parser
Thank You.

Split on symbols:
"10x-10y2+100x-100k=100".split(/[-+=]/);
Output:
["10x", "10y2", "100x", "100k", "100"]
If you need to use match() method I suggest same approach:
"10x-10y2+100x-100k=100".match(/[^-+=]+/g);
Output is the same.

/(?:0|[1-9]\d*)?(?:[a-z]+)?(?:\^(?:0|[1-9]\d*))?/g
// finds:
// vvv vvvvv vvvv vvvv vvvvv vv vvv v vvv
10x-10y^2+100x-100k=100^4+xy+100+100y2+0-1^0
// doesn't find: ^^^^^
(?:0|[1-9]\d*)? 0, OR 1-9 then zero or more numbers. Optional.
(?:[a-z]+)? Optional one or more lowercase letters.
(?:\^[1-9]\d*)? Optional power.
\^ Literal text.
(?:0|[1-9]\d*) Zero, OR 1-9 then zero or more numbers.
If I've missed anything let me know and I'll incorporate it.

Just try with following regex:
/(\d+[a-z]?\d*)/g
example

Try this:
s.match(/\d+([A-Za-z]\d*)?/g)

Related

Validation Regex for quantities with centesimal and without centesimal ...?

Hello to the community I have a query, I need a validation Regex, for amounts without decimals, that consider valid the following structure.
99,999,999
If I add a value:
12345678 -> Ok
12,345,678 -> Ok
123,456,789 -> Failed
123,45,6,78 -> Failed
12,345,678.50 -> Failed
12,456,7ab -> Failed
I have only been able to validate the size of 8 numerical characters:
var regex8 = /^-?([0-9]{1,8})?$/;
I wait for your comments.
Thank you.
With a bit of work you can craft a pattern to do this:
https://regex101.com/r/DKpSUR/1
/^-?([0-9]{1,2},?)?([0-9]{3},?){1,2}$/
This regex should supply you with what you want or point you in a direction:
-?([0-9]{1,2},)?([0-9]{3},)?[0-9]{3}
This will match an optional leading sign [-+]?
followed by either
a string of one or more digits \d+ or
a 1-3 digit string \d{1,3} followed by one more more groups of comma-3-digits ,\d{3}
Putting it all together:
/^[-+]?((\d+)|(\d{1,3}(,\d{3})+))$/
I have grouped them with parentheses () to make it clear, but be aware this creates capturing groups.
var rgx = /^[-+]?((\d+)|(\d{1,3}(,\d{3})+))$/
var matched = "+813,823".match(rgx); // ==> ["+813,823", "813,823", undefined, "813,823", ",823"]
You would want matched[0] to get the whole match.

Test if a sentence is matching a text declaration using regex

I want to test if a sentence like type var1,var2,var3 is matching a text declaration or not.
So, I used the following code :
var text = "int a1,a2,a3",
reg = /int ((([a-z_A-Z]+[0-9]*),)+)$/g;
if (reg.test(text)) console.log(true);
else console.log(false)
The problem is that this regular expression returns false on text that is supposed to be true.
Could someone help me find a good regular expression matching expressions as in the example above?
You have a couple of mistekes.
As you wrote, the last coma is required at the end of the line.
I suppose you also want to match int abc123 as correct string, so you need to include letter to other characters
Avoid using capturing groups for just testing strings.
const str = 'int a1,a2,a3';
const regex = /int (?:[a-zA-Z_](?:[a-zA-Z0-9_])*(?:\,|$))+/g
console.log(regex.test(str));
You will need to add ? after the comma ,.
This token ? matches between zero and one.
Notice that the last number in your text a3 does not have , afterward.
int ((([a-z_A-Z]+[0-9]*),?)+)$

Find file sequence with RegExp in Javascript

I have a simple question:
How do I use RegExp in Javascript to find strings that matches this filter:
*[0-9].png in order to filter out file sequences.
For example:
bird001.png
bird002.png
bird003.png
or
abc_1.png
abc_2.png
Should ignore strings like abc_1b.png and abc_abc.png
I'm going to use it in a getFiles function.
var regExp = new RegExp(???);
var files = dir.getFiles(regExp);
Thanks in advance!
EDIT:
If I have a defined string, let's say
var beginningStr = "bird";
How can I check if a string matches the filter
beginningStr[0-9].png
? And ideally beginningString without case sensitivity. So that the filter would allow Bird01 and bird02.
Thanks again!
Anything followed by [0-9] and ened by .png:
/^.*[0-9]\.png$/i
Or simply without begining (regex will find it itself):
/[0-9]\.png$/i
If I understood correctly, you need a regex that matches files with names which:
Begin with letters a-z, A-Z
Optionally followed with single _
Followed by one or more digits
Ending with .png
Regex for this is [a-zA-Z]_{0,1}+\d+\.png
You could try online regex builders which offer immediate explanation of what you write.
If I understood correctly,
var re = /\s[a-zA-Z]*[0-9]+\.png/g;
var filesArr = str.match(re);
filesArr.sort();// you can use own sort function
Please specify what is the dir variable

Javascript Regex - Get All string that has square brackets []

I have string data below:
var data = "somestring[a=0]what[b-c=twelve]----[def=one-2]test"
I need to get all strings that contain square brackets []. This is the result that I want.
["[a=0]", "[b-c=twelve]", "[def=one-2]"]
I've tried using regex /\[(.*?)\]/, but what I've got is an only the first array element is correct, the next elements are basically the same value but without the square brackets.
data.match(/\[(.*?)\]/);
// result => ["[a=0]", "a=0"]
What regexp should I use to achieve the result that I want? Thank you in advance.
You want to use the g (global) modifier to find all matches. Since the brackets are included in the match result you don't need to use a capturing group and I used negation instead to eliminate the amount of backtracking.
someVar.match(/\[[^\]]*]/g);
In /\[(.*?)\]/, *? means lazy, matching as few content as possible.
What you actually want is all the matches in content. Try modifier g
Try this one, http://regex101.com/r/aD6cM8/1. Any match starts with [, ends with ], but doesn't allow[ or ] inbetween.
someVar.match(/\[([^\[\]]*)\]/g)
You should just add the g switch to your regex :
someVar.match(/\[(.*?)\]/); // result => ["[a=0]", "a=0"]
results in
[ "[a=0]", "[b-c=twelve]", "[def=one-2]" ]
Your regex is correct, just suffix g to it to make it global:
someVar.match(/\[(.*?)\]/g);
Here's more info on it: http://www.w3schools.com/jsref/jsref_regexp_g.asp

java script Regular Expressions patterns problem

My problem start with like-
var str='0|31|2|03|.....|4|2007'
str=str.replace(/[^|]\d*[^|]/,'5');
so the output becomes like:"0|5|2|03|....|4|2007" so it replaces 31->5
But this doesn't work for replacing other segments when i change code like this:
str=str.replace(/[^|]{2}\d*[^|]/,'6');
doesn't change 2->6.
What actually i am missing here.Any help?
I think a regular expression is a bad solution for that problem. I'd rather do something like this:
var str = '0|31|2|03|4|2007';
var segments = str.split("|");
segments[1] = "35";
segments[2] = "123";
Can't think of a good way to solve this with a regexp.
Here is a specific regex solution which replaces the number following the first | pipe symbol with the number 5:
var re = /^((?:\d+\|){1})\d+/;
return text.replace(re, '$15');
If you want to replace the digits following the third |, simply change the {1} portion of the regex to {3}
Here is a generalized function that will replace any given number slot (zero-based index), with a specified new number:
function replaceNthNumber(text, n, newnum) {
var re = new RegExp("^((?:\\d+\\|){"+ n +'})\\d+');
return text.replace(re, '$1'+ newnum);
}
Firstly, you don't have to escape | in the character set, because it doesn't have any special meaning in character sets.
Secondly, you don't put quantifiers in character sets.
And finally, to create a global matching expression, you have to use the g flag.
[^\|] means anything but a '|', so in your case it only matches a digit. So it will only match anything with 2 or more digits.
Second you should put the {2} outside of the []-brackets
I'm not sure what you want to achieve here.

Categories

Resources