Dynamic Regex for extracting dot notion based variables - javascript

What Regex would I use to return all strings (in dot notation) that contain scope., but return full values including any number of dots that follow.
For example, the code below does not return the ".string" part.
> "scope.object.string".match(/(scope[.]\w+)/gi)
< ["scope.object"]
The code below will return "scope.object.object2" because I explicitly added a second [.]\w+, which is not dynamic.
> "scope.object.object2.string".match(/(scope[.]\w+[.]\w+)/gi)
< ["scope.object.object2"]
How would I do this dynamically so that I could get this value returned from this string:
> "scope.object.string scope.object.object2.string scope.object.object2.object3.string".match(/newRegex/)
< ["scope.object.string", "scope.object.object2.string", "scope.object.object2.object3.string"]
Even better if you can remove the "scope." part from each string with the same regex in the same call:
> "scope.object.string scope.object.object2.string scope.object.object2.object3.string".match(/newRegex/)
< ["object.string", "object.object2.string", "object.object2.object3.string"]

scope[.](?:\w+[.])*\w+
You can use this.If you want to removescope. the use
scope[.]((?:\w+[.])*\w+)
And grab the group 1 .See demo.
https://regex101.com/r/pT4tM5/24
var re = /scope[.]((?:\w+[.])*\w+)/gm;
var str = 'scope.object.object2.string\nscope.object.object2';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

Related

Replace content present in the nested brackets

Input = ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))
Expected Output = ABCDEF,GHIJKLMN,OPQRSTUVW
Tried so far
Output = Input.replace(/ *\([^)]*\)*/g, "");
Using a regex here probably won't work, or scale, because you expect nested parentheses in your input string. Regex works well when there is a known and fixed structure to the input. Instead, I would recommend that you approach this using a parser. In the code below, I iterate over the input string, one character at at time, and I use a counter to keep track of how many open parentheses there are. If we are inside a parenthesis term, then we don't record those characters. I also have one simple replacement at the end to remove whitespace, which is an additional step which your output implies, but you never explicitly mentioned.
var pCount = 0;
var Input = "ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))";
var Output = "";
for (var i=0; i < Input.length; i++) {
if (Input[i] === '(') {
pCount++;
}
else if (Input[i] === ')') {
pCount--;
}
else if (pCount == 0) {
Output += Input[i];
}
}
Output = Output.replace(/ /g,'');
console.log(Output);
If you need to remove nested parentheses, you may use a trick from Remove Nested Patterns with One Line of JavaScript.
var Input = "ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))";
var Output = Input;
while (Output != (Output = Output.replace(/\s*\([^()]*\)/g, "")));
console.log(Output);
Or, you could use a recursive function:
function remove_nested_parens(s) {
let new_s = s.replace(/\s*\([^()]*\)/g, "");
return new_s == s ? s : remove_nested_parens(new_s);
}
console.log(remove_nested_parens("ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3))"));
Here, \s*\([^()]*\) matches 0+ whitespaces, (, 0+ chars other than ( and ) and then a ), and the replace operation is repeated until the string does not change.

regular expressions- not including a character

I want to capture the value for name from query string with regular expression; I have done the folowing:
/name=(.*)/g
example: ?name=foo&bar=baz
But this grabs all string to the end; I know ^ is used for not; but I could not figure out the right syntax.
Thanks
If you want to use regex you can use a non greedy operator like this:
name=(.*?)&
Btw, you can also you another regex like this to cover more cases:
name=(.*?)(?:&|$)
Working demo
Javascript code:
var re = /name=(.*?)(?:&|$)/gm;
var str = 'example: ?name=foo&bar=baz\nexample: ?name=foo\nexample: ?bar=baz&name=foo';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
You can try /name=([^&]+)/ if you only have one or use global modifier for more

Javascript regex escape

I have a regular expression like this which extract the content between 2 characters and in this case its between 2 #'s
(?<=\#)(.*?)(?=\#)
and um using it as follows
var extract = str.match(/(?<=\#)(.*?)(?=\#)/).pop();
but the regex gives errors since I think I need to escape it. How do I correctly apply escape characters for the above regex?
Regex may be overkill for this task.
var result = str.split("#")[1] || "";
If there is no # in the string, result is the empty string.
If there is only one # in the string, result is everything after it.
If there are two or more # in the string, result is the substring between the first and second #.
#(.*?)#
or
#([^#]+)#
Simply use this and grab the group 1.See demo.
https://regex101.com/r/uE3cC4/14
var re = /#(.*?)#/gm;
var str = 'bazbarfoo#asad#';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

How can i extract only the words end with number?

i am getting a info from back-end. i need to convert that in to multiple word, and to require to store in array. using regexp how to get that?
here is the word:
Enter Room/Area^_^Area 100#_#Enter Grid^_^Grid2#_#Enter Level / Building^_^Building1#_#Enter Drawing^_^Drawing1#_#Enter Spec section^_^Spec2#_#Enter BOQ^_^BOQ1#_#
the result should be :
["Area 100", "Grid2","Building1", "Drawing1", "Spec2", "BOQ1" ]
So simply i would like to pick the words which end with number. upto the special char ^.
\b[a-zA-Z0-9 ]+\d+\b
Try this.See demo.
https://regex101.com/r/wZ0iA3/6
var re = /\b[a-zA-Z0-9 ]+\d+\b/gi;
var str = 'Enter Room/Area^_^Area 100#_#Enter Grid^_^Grid2#_#Enter Level / Building^_^Building1#_#Enter Drawing^_^Drawing1#_#Enter Spec section^_^Spec2#_#Enter BOQ^_^BOQ1#_#';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

Javascript match and regularexpression

I've searched for an example, but I'm so bad at Regular Expressions that I really need an answer to my specific example.
I'm using JavaScript and have the following string as an example:
accountActivityStatus.transactionHistorys.0.activityAmt
I need to be able to match that any given string starts with accountActivityStatus and contains a number somewhere after that.
Any assistance would be greatly appreciated:
^accountActivityStatus(?=.*\d).*$
Try this.See demo.
http://regex101.com/r/yZ7qJ6/7
var re = /^accountActivityStatus(?=.*\d).*$/gm;
var str = 'accountActivityStatus.transactionHistorys.0.activityAmt\naccountActivityStatus.transactionHistorys..activityAmt\naccountActivityStatus.transactionHistorys.4.activityAmt';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

Categories

Resources