How can i extract only the words end with number? - javascript

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.
}

Related

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.
}

Dynamic Regex for extracting dot notion based variables

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.
}

regex look-behind: Invalid regular expression: /(?<=\.)\S+$/: Invalid group

I have the following regexp
(?<=\.)\S+$
used to extract the extension (whatever comes after the last dot) of a string. regex101.com seems to accept my regexp: the string extension is correctly matched. As soon as I move it into a javascript script and try to test it against a string I get this error:
Invalid regular expression: /(?<=\.)\S+$/: Invalid group
I get the same error also with regex101 auto generated code:
var re = /(?<=\.)\S+$/;
var str = 'test.txt';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
see fiddle HERE
Any hint?
You cannot use look-behinds in JavaScript regex. There are some JavaScript look-behind workarounds, but they all have their own handicaps. To play it safe, use capturing groups. Here, the contents you need will be in the 2nd group:
(\.)([^.]+)$
Or, with just 1 capturing group (to increase performance):
\.([^.]+)$
Code:
var re = /\.([^.]+)$/;
var str = 'test.txt';
var m;
if ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
alert(m[1]);
}
Due to babel-loader error
Replace var re = /(?<=\.)\S+$/; with var re = new RegExp("(?<=\\.)\\S+$");

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