Match regex "labes" key/value pair - javascript

I try to write a regex rule that matches "labels" which are basically key=value pairs.
In key & value should only be alphanumeric values (and -) be allowed.
Thats what i have tried so far:/(-*.)=(-*.)/g
But it does not work with the input patter a-b=c-d, it does not match the "a" & "-d"
Valid input patterns:
a=b
1=1
a-b=c-d
a=b-c
Invalid input:
foo=bar
ba=r=b=az
b = z
a-b=c - d
te:st=st:ring
Notice the white space. White space in either key or value are invalid and only one = is allowed.
I created a example on: https://regex101.com/r/GNm5K7/1

You could write the pattern matching 1 or more alphanumerics or - in a character class using [a-zA-Z0-9-]+
^[a-zA-Z0-9-]+=[a-zA-Z0-9-]+$
See a regex101 demo.
If the - can not be at the start or at the end:
^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*=[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$
See another regex101 demo.

The regex I used is: /^[a-zA-Z0-9-]+=[a-zA-Z0-9-]+$/

Related

How to match regular expression In Javascript

I have string [FBWS-1] comes first than [FBWS-2]
In this string, I want to find all occurance of [FBWS-NUMBER]
I tried this :
var term = "[FBWS-1] comes first than [FBWS-2]";
alert(/^([[A-Z]-[0-9]])$/.test(term));
I want to get all the NUMBERS where [FBWS-NUMBER] string is matched.
But no success. I m new to regular expressions.
Can anyone help me please.
Note that ^([[A-Z]-[0-9]])$ matches start of a string (^), a [ or an uppercase ASCII letter (with [[A-Z]), -, an ASCII digit and a ] char at the end of the string. So,basically, strings like [-2] or Z-3].
You may use
/\[[A-Z]+-[0-9]+]/g
See the regex demo.
NOTE If you need to "hardcode" FBWS (to only match values like FBWS-123 and not ABC-3456), use it instead of [A-Z]+ in the pattern, /\[FBWS-[0-9]+]/g.
Details
\[ - a [ char
[A-Z]+ - one or more (due to + quantifier) uppercase ASCII letters
- - a hyphen
[0-9]+ - one or more (due to + quantifier) ASCII digits
] - a ] char.
The /g modifier used with String#match() returns all found matches.
JS demo:
var term = "[FBWS-1] comes first than [FBWS-2]";
console.log(term.match(/\[[A-Z]+-[0-9]+]/g));
You can use:
[\w+-\d]
var term = "[FBWS-1] comes first than [FBWS-2]";
alert(/[\w+-\d]/.test(term));
There are several reasons why your existing regex doesn't work.
You trying to match the beginning and ending of your string when you
actually want everything in between, don't use ^$
Your only trying to match one alpha character [A-Z] you need to make this greedy using the +
You can shorten [A-Z] and [0-9] by using the shorthands \w and \d. The brackets are generally unnecessary.
Note your code only returns a true false value (your using test) ATM it's unclear if this is what you want. You may want to use match with a global modifier (//g) instead of test to get a collection.
Here is an example using string.match(reg) to get all matches strings:
var term = "[FBWS-1] comes first than [FBWS-2]";
var reg1 = /\[[A-Z]+-[0-9]\]/g;
var reg2 = /\[FBWS-[0-9]\]/g;
var arr1 = term.match(reg1);
var arr2 = term.match(reg2)
console.log(arr1);
console.log(arr2);
Your regular expression /^([[A-Z]-[0-9]])$/ is wrong.
Give this regex a try, /\[FBWS-\d\]/g
remove the g if you only want to find 1 match, as g will find all similar matches
Edit: Someone mentioned that you want ["any combination"-"number"], hence if that's what you're looking for then this should work /\[[A-Z]+-\d\]/

Regex validate string if it does not contain more than special count of special characters

I'm developing a pattern that validates string if it does not contain more then two matches of #. here is code:
^[^\!|\#|\$|\%|\^|\&|\*|\+][\w]*([\w ]+\#[\w ]*){0,2}$
[^!|\#|\$|\%|\^|\&|*|+]
this is group of not acceptable symbols.
additionally, the pattern should validate string in case if it contains other symbols( - _ , . / ). each symbol should have it's own counter and should not match in any position more than two times.
for example if i have s string like this:
Mcloud dr. #33/#45, some text, some text
it should be valid. but in this case should not:
Mcloud dr. ###33/$#45, ####, ----
What would you suggest ?
Given that you want to match alphanumerics characters and some special symbols ()-_,./ You have to mention them in a character class like this.
Regex: ^(?!.*([(),.#/-])\1)([\w (),.#/-]+)$
Explanation:
(?!.*([(),.#/-])\1) asserts that there shouldn't be more than one character mentioned in character class. This asserts from beginning of string to end.
([\w (),.#/-]+) matches the rest of the string for allowed characters from beginning to end.
Regex101 Demo

Matching items in a comma-delimited list which aren't surrounded by single or double quotes

I'm wanting to match any instance of text in a comma-delimited list. For this, the following regular expression works great:
/[^,]+/g
(Regex101 demo).
The problem is that I'm wanting to ignore any commas which are contained within either single or double quotes and I'm unsure how to extend the above selector to allow me to do that.
Here's an example string:
abcd, efgh, ij"k,l", mnop, 'q,rs't
I'm wanting to either match the five chunks of text or match the four relevant commas (so I can retreive the data using split() instead of match()):
abcd
efgh
ij"k,l"
mnop
'q,rs't
Or:
abcd, efgh, ij"k,l", mnop, 'q,rs't
^ ^ ^ ^
How can I do this?
Three relevant questions exist, but none of them cater for both ' and " in JavaScript:
Regex for splitting a string using space when not surrounded by single or double quotes - Java solution, doesn't appear to work in JavaScript.
A regex to match a comma that isn't surrounded by quotes - Only matches on "
Alternative to regex: match all instances not inside quotes - Only matches on "
Okay, so your matching groups can contain:
Just letters
A matching pair of "
A matching pair of '
So this should work:
/((?:[^,"']+|"[^"]*"|'[^']*')+)/g
RegEx101 Demo
As a nice bonus, you can drop extra single-quotes inside the double-quotes, and vice versa. However, you'll probably need a state machine for adding escaped double-quotes inside double quoted strings (eg. "aa\"aa").
Unfortunately it matches the initial space as well - you'll have to the trim the matches.
Using a double lookahead to ascertain matched comma is outside quotes:
/(?=(([^"]*"){2})*[^"]*$)(?=(([^']*'){2})*[^']*$)\s*,\s*/g
(?=(([^"]*"){2})*[^"]*$) asserts that there are even number of double quotes ahead of matching comma.
(?=(([^']*"){2})*[^']*$) does the same assertion for single quote.
PS: This doesn't handle case of unbalanced, nested or escaped quotes.
RegEx Demo
Try this in JavaScript
(?:(?:[^,"'\n]*(?:(?:"[^"\n]*")|(?:'[^'\n]*'))[^,"'\n]*)+)|[^,\n]+
Demo
Add group for more readable (remove ?<name> for Javascript)
(?<has_quotes>(?:[^,"'\n]*(?:(?<double_quotes>"[^"\n]*")|(?<single_quotes>'[^'\n]*'))[^,"'\n]*)+)|(?<simple>[^,\n]+)
Demo
Explanation:
(?<double_quotes>"[^"\n]*") matches "Any inside but not "" = (1) (in double quote)
(?<single_quotes>'[^'\n]*') matches 'Any inside but not '' = (2) (in single quote)
(?:(?<double_quotes>"[^"\n]*")|(?<single_quotes>'[^'\n]*')) matches (1)or(2) = (3)
[^,"'\n]* matches any text but not "', = (w)
(?:(?:(?<double_quotes>"[^"\n]*")|(?<single_quotes>'[^'\n]*'))[^,"'\n]*) matches (3)(w)
(?:(?:(?<double_quotes>"[^"\n]*")|(?<single_quotes>'[^'\n]*'))[^,"'\n]*)+ matches repeat (3)(w) = (3w+)
(?<has_quotes>[^,"'\n]*(?:(?:(?<double_quotes>"[^"\n]*")|(?<single_quotes>'[^'\n]*'))[^,"'\n]*)+) matches (w)(3w+) = (4) (has quotes)
[^,\n]+ matches other case (5) (simple)
So in final we have (4)|(5) (has quote or simple)
Input
abcd,efgh, ijkl
abcd, efgh, ij"k,l", mnop, 'q,rs't
'q, rs't
"'q,rs't, ij"k, l""
Output:
MATCH 1
simple [0-4] `abcd`
MATCH 2
simple [5-9] `efgh`
MATCH 3
simple [10-15] ` ijkl`
MATCH 4
simple [16-20] `abcd`
MATCH 5
simple [21-26] ` efgh`
MATCH 6
has_quotes [27-35] ` ij"k,l"`
double_quotes [30-35] `"k,l"`
MATCH 7
simple [36-41] ` mnop`
MATCH 8
has_quotes [42-50] ` 'q,rs't`
single_quotes [43-49] `'q,rs'`
MATCH 9
has_quotes [51-59] `'q, rs't`
single_quotes [51-58] `'q, rs'`
MATCH 10
has_quotes [60-74] `"'q,rs't, ij"k`
double_quotes [60-73] `"'q,rs't, ij"`
MATCH 11
has_quotes [75-79] ` l""`
double_quotes [77-79] `""`

Comma separated numbers regex - accepts pipe character

I have written a small regex for javascript. It should only accept numbers separated by commas.
Valid examples are:
1 single value allowed
1,278,3780,50
1,56,90, (trailing comma allowed)
Invalid examples are:
1,45 67
1, gj, + (any special character and characters)
The regex is: /^[\d|\,]+/g
However, it also accepts | (pipe character).
Like: 1|46|6778|567
What am I doing wrong? What did I miss?
Please follow this link to my regex
You don't need pipe (|) and escaping characters within character class. Also as a proper way you can use following regex:
/^(?:\d+\,)+\d+$/g
Debuggex Demo
As i missed your edit if the trailing comma is a valid case you can simply use following regex :
^(\d+,?)+$
Try this -
^\d+\,(?:\d+\,?)+$
Demo
EDIT:
With changed requirements -
^\d+(?:,\d+)*,?$
Demo here
To match a number separated by comma:
(\d+,?)+
The correct regex is as follows:
^\d+(?:,\d+)*,?$
This will match the cases specified:
A single number (1)
A series of numbers delimited by commas (1,2,3)
An optional trailing comma (1,, 1,2,3,4,)

Help interpreting a javascript Regex

I have found the following expression which is intended to modify the id of a cloned html element e.g. change contactDetails[0] to contactDetails[1]:
var nel = 1;
var s = $(this).attr(attribute);
s.replace(/([^\[]+)\[0\]/, "$1["+nel+"]");
$(this).attr(attribute, s);
I am not terribly familiar with regex, but have tried to interpret it and with the help of The Regex Coach however I am still struggling. It appears that ([^\[]+) matches one or more characters which are not '[' and \[0\]/ matches [0]. The / in the middle I interpret as an 'include both', so I don't understand why the author has even included the first expression.
I dont understand what the $1 in the replace string is and if I use the Regex Coach replace functionality if I simply use [0] as the search and 1 as the replace I get the correct result, however if I change the javascript to s.replace(/\[0\]/, "["+nel+"]"); the string s remains unchanged.
I would be grateful for any advice as to what the original author intended and help in finding a solution which will successfully replace the a number in square brackets anywhere within a search string.
Find
/ # Signifies the start of a regex expression like " for a string
([^\[]+) # Capture the character that isn't [ 1 or more times into $1
\[0\] # Find [0]
/ # Signifies the end of a regex expression
Replace
"$1[" # Insert the item captured above And [
+nel+ # New index
"]" # Close with ]
To create an expression that captures any digit, you can replace the 0 with \d+ which will match a digit 1 or more times.
s.replace(/([^\[]+)\[\d+\]/, "$1["+nel+"]");
The $1 is a backreference to the first group in the regex. Groups are the pieces inside (). So, in this case $1 will be replaced by whatever the ([^\[]+) part matched.
If the string was contactDetails[0] the resulting string would be contactDetails[1].
Note that this regex only replaces 0s inside square brackets. If you want to replace any number you will need something like:
([^\[]+)\[\d+\]
The \d matches any digit character. \d+ then becomes any sequence of at least one digit.
But your code will still not work, because Javascript strings are immutable. That means they can't be changed once created. The replace method returns a new string, instead of changing the original one. You should use:
s = s.replace(...)
looks like it replaces arrays of 0 with 1.
For example: array[0] goes to array[1]
Explanation:
([^[]+) - This part means save everything that is not a [ into variable $1
[0]/ - This part limits Part 1 to save everything up to a [0]
"$1["+nel+"]" - Print out the contents of $1 (loaded from part 1) and add the brackets with the value of nel. (in your example nel = 1)
Square braces define a set of characters to match. [abc] will match the letters a, b or c.
By adding the carat you are now specifying that you want characters not in the set. [^abc] will match any character that is not an a, b or c.
Because square braces have special meaning in RegExps you need to escape them with a slash if you want to match one. [ starts a character set, \[ matches a brace. (Same concept for closing braces.)
So, [^\[]+ captures 1 or more characters that are not [.
Wrapping that in parenthesis "captures" the matched portion of the string (in this case "contactDetails" so that you can use it in the replacement.
$1 uses the "captured" string (i.e. "contactDetails") in the replacement string.
This regex matches "something" followed by a [0].
"something" is identified by the expression [^\[]+ which matches all charactes that are not a [. You can see the () around this expression, because the match is reused with $1, later. The rest of your regex - that is \[0\] just matches the index [0]. The author had to write \[ and \] because [ and ] are special charactes for regular expressions and have to be escaped.
$1 is a reference to the value of the first paranthesis pair. In your case the value of
[^\[]+
which matches one or more characters which are not a '['
The remaining part of the regexp matches string '[0]'.
So if s is 'foobar[0]' the result will be 'foobar[1]'.
[^\[] will match any character that is not [, the '+' means one or more times. So [^[]+ will match contactDetails. The brackets will capture this for later use. The '\' is an escape symbol so the end \[0\] will match [0]. The replace string will use $1 which is what was captured in the brackets and add the new index.
Your interpretation of the regular expression is correct. It is intended to match one or more characters which are not [, followed by a literal [0]. And used in the replace method, the match would be replaced with the match of the first grouping (that’s what $1 is replaced with) together with the sequence [ followed by the value of nel and ] (that’s how "$1["+nel+"]" is to be interpreted).
And again, a simple s.replace(/\[0\]/, "["+nel+"]") does the same. Except if there is nothing in front of [0], because in that case the first regex wouldn’t find a match.

Categories