RegExp to match dot but not i.e or e.g - javascript

I'd like the match the third dot in this string
"Test i.e and some more e.g. And"
So, find the first dot that isn't "i.e" or "e.g"
So far, I have
(?!i\.e|e\.g)(\.)
But it still seems to be capturing all dots

Different ideas...
1.) To match dot at a non-word boundary
\.\B
See demo at regex101
2.) Or if it is always the very last character, just use end anchor:
\.$
Demo at regex101
3.) But if you want to match the last dot with characters ahead, use a lookahead.
\.(?![^.]*\.)
At any dot looks if not another dot is ahead (with any amount of [^.]* non-dots in between).
Demo at regex101

Javascript does not support look-behinds, this will be a hard task to do with the language. If you ask me, I would suggest traversing the text stream yourself and getting the dots, this is very simple to do and would probably be a lot faster than regexps.

Related

Regex finding file names

Can anyone help me with the REGEX to match
../_assets/applications/cleaning/*logo.png
"*" being the file name which can also follow an underscore or dash so
../_assets/applications/cleaning/main_logo.png
OR
../_assets/applications/cleaning/main-logo.png
this is as far as I got
\assets\/applications\/cleaning\/
An asterisk in a regex is a quantifier allowing zero or more of the previous character/group. So you first expression would allow zero or more forward slashes. You can use a . with a * to allow for zero or more of any character (excluding new line). So something like:
\/cleaning\/(.+?logo\.png)$
should find all the images you want, then:
/logos/$1
should replace them as you wanted.
Demo: https://regex101.com/r/dmAjjv/1/

Regex to match phrases not containing a palindrome

Is there a way to match a word not containing a palindrome (be it as long as it may)?
For instance, for a 6-character-long palindrome, foo/bar would match but xbarrabzz/1xoxxoxa14 would not match.
Use a negative lookahead, for example for length 5/6 (3-letter with middle letter reused or doubled):
^(?:(.)(?!(.)(.)\3?\2\1))*$
See live demo.
But you would have to add another look ahead for each length (which I leave as an exercise for the reader).
You can use \b(?:(?!(\w)(\w)\2?\1)\w)+\b.
Online Demo.
It's a simple negative lookahead that checks if the word contains a structure like xyx or xyyx.

Match full sentences skipping spurious dots

I need to match complete sentences ending at the full stop, but I'm stuck on trying to skip false dots.
To keep it simple, I've started with this syntax [^.]+[^ ] which works fine with normal sentences, but, as you can see, it breaks at every dots.
My regex101
So, at the first sentence, the result should be:
Recent studies have described a pattern associated with specific object (e.g., face-related and building-related) in human occipito-temporal cortex.
and so on.
Just use a lookahead to set the condition as match upto a dot which must be followed by a space or end of the line anchor $.
(.*?\.)(?=\s|$)
DEMO
Expanding upon this, here is a regex that doesn't use reluctant matching and potentially more efficient:
(?:[^.]+|\.\S)+\.
And if you would like to match the sentences themselves, and remove the one trending space that you would get from using the regex of the accepted answer, you can use this:
\S(?:[^.]+|\.\S)+\.
Here is a regex demo.

Regex for multiple phrases in one phrase

I really have no other way of explaining this however, I need a regular expression to match in javascript.
I have a spam prevention to match a string however, I would like multiple guesses and match those using regex.
As an example, I want to match thiswordhere. To return true, the matches can be:
thiswordhere
thisword
wordhere
word
It must be a standalone word so no spaces and no responses like word word or thisword word
Four possible outcomes. I'm very new to regex and all I could get using regexr is:
/(thisword)[here]/g
Which couldn't do the trick. I'm going to be studying regex a lot these coming months so I would like to see the solution for this example.
To match only that text on a line (or in your string), you can simply add the start ^ and end $ delimiters to your regex:
/^((?:this)?word(?:here)?)$/g
Example
A quick and easy way is to specify those particular outcomes as options:
/(thiswordhere|thisword|wordhere|word)/g
Example
A slightly better option might be to specify that the "word" part is always needed, with "this" optional on the left, and "here" optional on the right:
/((?:this)?word(?:here)?)/g
Example
FYI - your regex is saying match "thisword" literally, followed by any of the four characters "h", "e", "r", "e". What you need to say is "match word, optionally preceded by "this" and optionally followed by "here" (example above).
You can use a regex like this:
\b(?:this)?word(?:here)?\b
Working demo

Capture everything between constants

I want to capture everything between every instance of User. and a space, including User.
So given a test string of
psdojfsdf User.sdoinwpoiev.spoinwelsdknonfsjfnw ldnkfwwdf sdf User.sdoinffon.ribwgg
I want it to capture User.sdoinwpoiev.spoinwelsdknonfsjfnw and User.sdoinffon.ribwgg
I've gotten this far: /(User\..*)\s/, but this captures everything until the last space.
The way I believe is best is to tell it to match everything but space rather than everything. That gives:
/(User\.\S*)/
Another alternative is to use a non-greedy match, but I think that's less clear:
/(User\..*?)\s/
use a non-greedy quantifier:
/(User\..*?)\s/
See regular-expressions.info for details about greediness of repetition operators.
Note that this won't work if the word ends at the end of the input string, if there's no space at the end. Coenwulf's answer may be better, as it doesn't have this problem.
Use the *? non-greedy zero or more
/User\.[^ \s]*?/g
Also if you want to force it to have something between the dot and the space
/User\.[^ \s]+?/g
Or if you want it to be alphanumeric
/User\.[a-zA-Z_$]+?[a-zA-Z_$0-9]*?( | |\s)/g
If you want to allow line breaks between the dot and the property identifier
/User\.[a-zA-Z_$]+?[a-zA-Z_$0-9]*?(\n| | |\s)/gm

Categories