Regex for comma separated 3 letter words - javascript

I want to create a regex for exactly 3 letter words only, separated by commas. 3 letter words can be padded with space(at most 1 on each side)
Valid Examples:
ORD
JFK, LAX
ABC,DEF, GHK,REW, ASD
Invalid Examples:
ORDA
OR
ORD,
JFK, LA
I tried the following but couldn't get it to work.
^(?:[A-Z ]+,)*[A-Z ]{3} +$

Try this: ^([ ]?[A-Z]{3}[ ]?,)*([ ]?[A-Z]{3}[ ]?)+$
https://regex101.com/r/HFeN0D/2/
It matches at least one three letter word (with spaces), preceded by any number of words three letter words with commas after them.

Try this pattern:
^[A-Z]{3}(?:[ ]?,[ ]?[A-Z]{3})*$
This pattern matches an initial three letter word, followed by two more terms separated by a comma with optional spaces.

You can do this with the pattern: ^((:? ?[A-Z]{3} ?,)*(?: ?[A-Z]{3} ?))+$
var str = `ORD
JFK, LAX
ABC,DEF, GHK,REW, ASD
ORDA
OR
ORD,
JFK, LA`;
let result = str.match(/^((:? ?[A-Z]{3} ?,)*(?: ?[A-Z]{3} ?))+$/gm);
document.getElementById('match').innerHTML = result.join('<br>');
<p id="match"></p>

Related

Regex - to extract text before the last a hyphen/dash

Example data expected output
sds-rwewr-dddd-cash0-bbb cash0
rrse-cash1-nonre cash1
loan-snk-cash2-ssdd cash2
garb-cash3-dfgfd cash3
loan-unwan-cash4-something cash4
The common pattern is here, need to extract a few chars before the last hyphen of given string.
var regex1= /.*(?=(?:-[^-]*){1}$)/g ; //output will be "ds-rwewr-dddd-cash0" from "sds-rwewr-dddd-cash0-bbb "
var regex2 = /\w[^-]*$/g ; //output will be "cash0" from "ds-rwewr-dddd-cash0"
var res =regex2.exec(regex1.exec(sds-rwewr-dddd-cash0-bbb)) //output will cash0
Although above nested regex is working as expected but may not be optimize one. So any help will be appreciated for optimized regex
You can use
/\w+(?=-[^-]*$)/
If the part before the last hyphen can contain chars other than word chars, keep using \w[^-]*: /\w[^-]*(?=-[^-]*$)/. If you do not need to check the first char of your match, simply use /[^-]+(?=-[^-]*$)/.
See the regex demo.
Details:
\w+ - one or more word chars
(?=-[^-]*$) - that must be followed with - and then zero or more chars other than - till the end of string.
JavaScript demo
const texts = ['sds-rwewr-dddd-cash0-bbb','rrse-cash1-nonre','loan-snk-cash2-ssdd','garb-cash3-dfgfd','loan-unwan-cash4-something'];
const regex = /\w+(?=-[^-]*$)/;
for (var text of texts) {
console.log(text, '=>', text.match(regex)?.[0]);
}

How to get the first alphabetic word of string in javascript

I am trying to retrieve the first alphabetic word of a string, which might include tags as well.
I have tried using split(" ") but it gives me the spaces.
let letter = ' <section class="contact" id="contact">';
let firstWord = letter.split (" ");
It should just show section as the first word. Is there any way I can do. Thank you
Simple regex to match alphabetic (not alphanumeric) words /[a-zA-Z]+/g
let letter = ' <section class="contact" id="contact">';
let words = letter.match (/[a-zA-Z]+/g); // Match all alphabet
let firstWord = words.length > 0 ? words[0] : '';
console.log(firstWord);
You may use several solutions based on what you really need.
For the current scenario, you may match a chunk of 1+ ASCII letters
let letter = ' <section class="contact" id="contact">';
let first_word = (letter.match(/[a-z]+/i) || [""])[0];
console.log(first_word)
You may tell the regex engine to only match it if there are no digits or underscores around it using \b, word boundaries:
/\b[a-z]+\b/i
And in case you want to match any Unicode letter word and target ECMAScript 2018 and newer, you may use
let regex = /\p{Alphabetic}+/u;
console.log("Один,два".match(regex)[0]); // => Один
Or, with Unicode word boundaries,
let regex = /(?<![\p{Alphabetic}\p{N}_])\p{Alphabetic}+(?![\p{Alphabetic}\p{N}_])/u;
// Or,
// let regex = /(?<!\p{L}\p{M}*|[\p{N}_])\p{Alphabetic}+(?![\p{L}\p{N}_])/u
console.log("1Один2,два-три".match(regex)[0]); // => два
That is, to match 1+ alphabetic chars not preceded nor followed with letters or digits.
To get the first word, match a non-letter, then one or more letters inside a capturing group, then another non-letter:
let letter = ' <section class="contact" id="contact">';
let [, firstWord] = letter.match(/[^a-z]([a-z]+)[^a-z]/i);
console.log(firstWord);

Javascript Regex: Capture between two asterisks with multiple asterisks in comma delimited string

I am trying to capture all characters between multiple instances of asterisks, which are comma delimited in a string. Here's an example of the string:
checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*
The caveat is that the phrase must start and end with an asterisk. I have been able to come close by using the following regex, however, it won't discard any matches when the captured string is missing the starting asterisk(*):
let str = "checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*"
const regex = /[^\,\*]+(?=\*)/gi;
var a = str.match(regex)
console.log(a) // answer should exclude checkboxID0 and checkboxID5
The answer returns the following, however, "checkboxID0 and checkboxID5" should be excluded as it doesn't start with an asterisk.
[
"checkboxID0",
"checkboxID1",
"checkboxID3",
"checkboxID5"
]
Thanks, in advance!
You need to use asterisks on both ends of the pattern and capture all 1 or more chars other than commas and asterisks in between:
/\*([^,*]+)\*/g
See the regex demo
Pattern details
\* - an asterisk
([^,*]+) - Capturing group 1: one or more chars other than , and *
\* - an asterisk
JS demo:
var regex = /\*([^,*]+)\*/g;
var str = "checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*";
var m, res = [];
while (m = regex.exec(str)) {
res.push(m[1]);
}
console.log(res);

Javascript Remove all periods preceded by a character

I have a price string i.e: Rs. 1.2345,99
I need to remove all letters and all periods preceded by a letter.
So a string that was Rs. .1245.3445. s. 34. ffgg. would result in .1245.3445 34.
My expected result is 1.2345,99
I should add that in the case of Rs. .954 I also expect a result of .954
This is what I have tried:
string.replace(/[^0-9+-]|(?!^)[+-]/g, '');
But it does ALL of the decimals. Regex is NOT my strong point.
I need this to work with ANY strings not specifically this example string.
Best to break it down into multiple replaces.
First remove letters and periods that follow letters, then you can clean up whitespace:
var x = "Rs. 1.2345,99";
x = x.replace(/[\a-zA-Z]+[\.]*/g,"");
x = x.replace(/\s/g, "");
console.log(x);
Note that you only mention letters and periods, if you need other characters removing too then include then in the [\a-zA-Z] part.
This should do
var str = "Rs. 1.2345,99";
str = str.slice(4, str.length);
console.log(str);

Check if the text 'ABC' is there it should be preceded and succeeded by a numeric only, followed by a space

I need a regex which should fail for the below
text1 abc
abc 1ed1
text1 abc 1ed1
It should only pass the structures like
11231 abc 23435
Format: xxx abc yyy
xxx, yyy = digits only of any length
abc = Fixed string, it should be ignored if not appeared. If appeared, it should be in specific format.
I have tried the below regex. It passes if preceded and succeeded without any single digit only. But no luck with the above issues.
\b((\D+[ ]\bABC\b)|(\bABC\b[ ]\D+))
DETAILS FROM CHAT:
I don't have a string exactly. It's a user input. When the input value will contain something like 'abc' we should expect a numeric in both the sides. Else when it doesn't contain 'abc' we are good (may be some other validations).
Few examples :
123 abc (match)
abc 1234 (match)
xyz abc (match)
abc xyz (match)
xyz32 abc xyz23 (match)
xyz32 abc 34523 (match)
1234 abc xyz23 (match)
12322 abc 1111(pass, we are good).
All can be a sub-string, but new words i.e. not a part of the word
\d+\sabc+\s\d+
See https://regex101.com/r/gxgEGH/1 for explanation
\b\d+\s+\w+\s+\w+\b
console.log('text1 abc abc 1ed1 text1 abc 1ed1 11231 abc 23435'.match(/\b\d+\s+\w+\s+\w+\b/g));
You cannot achieve that with pure regex in JS as it does not support an infinite width lookbehind that would be necessary to check if abc is not preceded with a whole word consisting of only digits. A working approach can be the following: use an alternation group to capture the abc within digits as whole words, and just match other cases where abc is a whole word. Then, check if the capture group matched, and if it did you have no match. Else, you have a match.
/\b(\d+ abc \d+)\b|\babc\b/
Details:
\b(\d+ - a leading word boundary (there can be start of string or a non-word char before) 1+ digits (also, start of Group 1)
abc - abc hard-coded value with spaces around
\d+)\b - 1+ digits followed with a trailing word boundary (no word chars - letters, digits or underscores - can appear after these digits) (end of Group 1)
| - or
\babc\b - a whole word abc
See the regex demo. Note that last match also contains a capture group #1.
var regex = /\b(\d+ abc \d+)\b|\babc\b/;
var strs = ['123 abc', 'abc 1234', 'xyz abc', 'abc xyz', 'xyz32 abc xyz23', 'xyz32 abc 34523', '1234 abc xyz23', '12322 abc 1111'];
for (var i=0; i<strs.length; i++) {
var m = strs[i].match(regex);
if (m && m[1])
console.log(strs[i] + " is FALSE");
else if (m)
console.log(strs[i] + " is TRUE");
}

Categories