Regex to find only colon in "asdf:adsf" but not "asdf: adsf" - javascript

I'm trying to use regex to capture ONLY the colon in something like asdf:adsf, which could also look like a:b, asdf:b, b:asdf,123:b,2:1, etc. However, it SHOULD NOT match asdf: adsf, a: b, asdf :b, b: asdf, 123 : b, 2: 1,
So far I have tried a slew of different regexes, and the closest I've come is:
\x20*:\x20* // wrong because this captures all whitespace

\S(:)(?=\S)
Try this.Grab the capture or group.See demo.
https://regex101.com/r/fA6wE2/29
var re = /\S(:)(?=\S)/gm;
var str = 'a:b\nasas:b\na:sadasd\na: sdffds\na :asfsd\na : dsfdf';
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.
}

\w+(?=\S):(?=\S)\w+
This will solve the problem.
My other answer was deleted with that regex posted before.

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

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+$");

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