Given a string like this:
"fieldNameA[fieldValueA] fieldNameB[fieldValueB] fieldNameC[fieldValueC]"
I'm looking for a regular expression (or two) that will allow me to construct a set of key value pairs e.g.
fieldNameA : "fieldValueA"
fieldNameB : "fieldValueB"
fieldNameC : "fieldValueC"
Values will always be in square brackets.
Square brackets will always be preceded by the field name
Field names could vary in length
I tried something like:
const reg = new RegExp("fieldNameA|fieldNameB|fieldNameC\[(.*?)\]", "gi");
const matches = reg.exec(query);
But this doesn't work. I just get an array with one value: "fieldNameA"
You may use the following solution:
var query = "fieldNameA[fieldValueA] fieldNameB[fieldValueB] fieldNameC[fieldValueC]";
var matches = {}, m;
var reg = /(\w+)\[(.*?)]/gi;
while(m=reg.exec(query)) {
matches[m[1]] = m[2];
}
console.log(matches);
You need to use a regex literal, or "\[" will turn into "[" and will no longer match a literal [.
Also, to match all occurrences, you need to run a RegExp#exec in a loop.
Pattern details:
(\w+) - Group 1: one or more word chars (if you want to match an identifier that cannot start with a digit, replace with [_a-zA-Z]\w*)
\[ - a literal [
(.*?) - Group 2: any 0+ chars other than line break chars (if you need to match any char but ], use [^\]]*)
] - a literal ] char.
Related
I'm using JavaScript for parsing a string that looks as follow:
var myString = "unimportant:part.one:unimportant:part.two:unimportant:part.three";
var regex = /\w*:(part.\w*)./gi
How can I put only the highlighted part within the parenthesis in an array?
var myArray = myString.match(regex); gives me the whole line.
In your pattern \w*:(part.\w*). the leading \w* is optional so if you want to match at least a single word character you can just use \w
After the capture group there is a dot . which matches any character so you will miss the last character at the end of the string as the character is mandatory.
Note to escape the dot \. if you want to match it literally
The pattern can look like:
\w:(part\.\w+)
Then getting the capture group 1 values into an array using matchAll
const myString = "unimportant:part.one:unimportant:part.two:unimportant:part.three";
const regex = /\w:(part\.\w+)/gi;
const result = Array.from(myString.matchAll(regex), m => m[1])
console.log(result)
Or without a capture group using a lookbehind if that is supported using match
const myString = "unimportant:part.one:unimportant:part.two:unimportant:part.three";
const regex = /(?<=\w:)part\.\w+/gi;
console.log(myString.match(regex))
I want to remove letters from (a1-800-b-400), but naively doing so results in (1-800--400).
How do I get (1-800-400)?
If it involves RegExp, please explain how it works.
This is the RegExp you need:
/[^\d\()-]/g
/ / are simply indicators that you're writing a RegExp literal.
The trailing g flag means find all matches, instead of just one.
Everything between the [ ] are the characters you want to match.
But the ^ symbol means the opposite; don't match.
\d means Number, \( means open parenthesis (escaped), ) is obvious, as is -.
const regExpA = /[^\d\()-]/g;
const regExpB = /--/g;
const string = '(a1-800-b-400)';
const result = string
.replace(regExpA, '') // (1-800--400)
.replace(regExpB, '-'); // (1-800-400)
console.log(result);
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\]/
I'd like to use regex to match the last mathematical operator in a calculation string.
For example, In the string:
"25+4-1*100/423", it will match '/'
Or
"25*2*4444*123+4", it will match '+'
I have tried the regex /(\-|\+|\/|\*)(?=[^\-\+\/\*]*$)/, but for some reason it matches twice the last operator twice.
For example:
var str = "25+4-1*3"
var regex = /(\-|\+|\/|\*)(?=[^\-\+\/\*]*$)/
str.match(regex) = ["*", "*"]
Your regex works absolute fine. You just need to add a global (g) flag when matching the string.
var str = "25+4-1*3";
var result = str.match(/(\-|\+|\/|\*)(?=[^\-\+\/\*]*$)/g);
console.log(result);
but for some reason it matches twice the last operator twice.
var str = "25+4-1*3" when I type str.match(regex) I get ["*", "*"]
RegExp#exec and String#match both return an array of results with the overall match (for the whole expression) as the first entry and then the values of any capture groups as subsequent entries. Since you have a capture group, that means you see the operator in both the first and second positions.
Just remove the capture group (make it a non-capturing group), as the whole expression other than the lookahead is the capture group:
var regex = /(?:\-|\+|\/|\*)(?=[^\-\+\/\*]*$)/;
// ^^^^^^^^^^^^^^^---- non-capturing group
var str = "25+4-1*3"
console.log(str.match(regex));
If for some reason you needed the capture group (e.g., if there were more to the expression than just the lookahead), you'd keep it but then just ignore the first entry in the array (if you only wanted the capture).
Here is a string str = '.js("aaa").js("bbb").js("ccc")', I want to write a regular expression to return an Array like this:
[aaa, bbb, ccc];
My regular expression is:
var jsReg = /.js\(['"](.*)['"]\)/g;
var jsAssets = [];
var js;
while ((js = jsReg.exec(find)) !== null) {
jsAssets.push(js[1]);
}
But the jsAssets result is
[""aaa").js("bbb").js("ccc""]
What's wrong with this regular expression?
Use the lazy version of .*:
/\.js\(['"](.*?)['"]\)/g
^
And it would be better if you escape the first dot.
This will match the least number of characters until the next quote.
jsfiddle demo
If you want to allow escaped quotes, use something like this:
/\.js\(['"]((?:\\['"]|[^"])+)['"]\)/g
regex101 demo
I believe it can be done in one-liner with replace and match method calls:
var str = '.js("aaa").js("bbb").js("ccc")';
str.replace(/[^(]*\("([^"]*)"\)[^(]*/g, '$1,').match(/[^,]+/g);
//=> ["aaa", "bbb", "ccc"]
The problem is that you are using .*. That will match any character. You'll have to be a bit more specific with what you are trying to capture.
If it will only ever be word characters you could use \w which matches any word character. This includes [a-zA-Z0-9_]: uppercase, lowercase, numbers and an underscore.
So your regex would look something like this :
var jsReg = /js\(['"](\w*)['"]\)/g;
In
/.js\(['"](.*)['"]\)/g
matches as much as possible, and does not capture group 1, so it matches
"aaa").js("bbb").js("ccc"
but given your example input.
Try
/\.js\(('(?:[^\\']|\\.)*'|"(?:[\\"]|\\.)*"))\)/
To break this down,
\. matches a literal dot
\.js\( matches the literal string ".js("
( starts to capture the string.
[^\\']|\\. matches a character other than quote or backslash or an escaped non-line terminator.
(?:[\\']|\\.)* matches the body of a string
'(?:[\\']|\\.)*' matches a single quoted string
(...|...) captures a single quoted or double quoted string
)\) closes the capturing group and matches a literal close parenthesis
The second major problem is your loop.
You're doing a global match repeatedly which makes no sense.
Get rid of the g modifier, and then things should work better.
Try this one - http://jsfiddle.net/UDYAq/
var str = new String('.js("aaa").js("bbb").js("ccc")');
var regex = /\.js\(\"(.*?)\"\){1,}/gi;
var result = [];
result = str.match (regex);
for (i in result) {
result[i] = result[i].match(/\"(.*?)\"/i)[1];
}
console.log (result);
To be sure that matched characters are surrounded by the same quotes:
/\.js\((['"])(.*?)\1\)/g