Replace Numbers with dots - javascript

I am trying to replace some ID numbers in my system to clickable number to open the related record. The problem is, that they are sometimes in this format: 123.456.789.
When I use my regex, I can replace them and it works fine. The problem accurse when I also have IP addresses where the regex also matches: 123.[123.123.123] (the [] indicates where it matches).
How I can I prevent this behavior?
I tried something like this: /^(?!\.)([0-9]{3}\.[0-9]{3}\.[0-9]{3})(?!\.)/
I am working on "notes" in a ticket system. When the note contains only the ID or an IP, the regexp is working. When it contains more text like:
Affected IDs:
641.298.855 (this, lead)
213.794.868
948.895.285
Then it is not matching anymore on my IDs. Could you help me with this issue and explain what I am doing wrong?

Add gm modifier:
/^(?!\.)([0-9]{3}\.[0-9]{3}\.[0-9]{3})(?!\.)/gm
https://regex101.com/r/pK1fV4/2

You don't need to use negative lookahead at the start and also you don't need to include g modifier, just m modifier would be enough for this case because ^ matches the start of a line and the following pattern will match the string which exists only at the start so it won't do any global match (ie, two or more matches in a single line).
/^([0-9]{3}\.[0-9]{3}\.[0-9]{3})(?!\.)/m
For the sake of performance, you further don't need to use capturing group.
/^[0-9]{3}\.[0-9]{3}\.[0-9]{3}(?!\.)/m

Related

How to find specific character with x times repeated, and after that character capture everything?

Have statement like [[+pagetitle]] and [[++site_name]], needed separate and find each individually.
I expecting to get [[+pagetitle]] and [[++site_name]] separately with 2 different of course regexp.
Tried:
\[{2}\+{1}.*?\]{2}
\[{2}\+{1}.+\]{2}
What i currently achieved, is collecting everything. \[{2}\+{1}.*?\]{2} with capture same as + and ++ as the same, but they have different ideas behind, how to find each separate? Regex101 page https://regex101.com/r/uezzxc/1
I expecting to get [[+pagetitle]] and [[++site_name]] separately with 2 different of course regexp
Using \[{2}\+[^+]*?\]{2} you can match [[+pagetitle]] and not [[++site_name]] because [^+] matches anything but a + sign.
To match [[++site_name]] and not [[+pagetitle]] you could use \[{2}\+{2}.*?\]{2}.
https://regex101.com/r/acgtj6/1

Regex for specific strings/paths

I need a regular expression to match many specific paths/strings but I can't figure it out.
E.g.
../foo/hoo/something.js -> Needs to match ../foo/hoo/
../foo/bar/somethingElse.js -> Needs to match ../foo/bar/
../foo/something-else.js -> Needs to match ../foo/
What I tried with no luck is the following regex:
/\..\/foo\/|bar\/|hoo\//g
This should work out for you:
/(\.\.\/foo\/(hoo\/|bar\/)?)/
https://regex101.com/r/1aTf7y/1
So you select ../foo/ at first and then have a group that can either contain hoo/ or bar/. And the question mark allows 0 or one instances.
If you want to be a little less specific, you could also do
/(\.\.\/[^\/]+\/(hoo\/|bar\/)?)/
The [^\/]+ allows all characters except for a slash
You can use the regex
(\/[^\/\s]+)+(?=\/)
see the regex101 demo
function match(str){
console.log(str.match(/(\/[^\/\s]+)+(?=\/)/)[0]);
}
match('./foo/hoo/something.js');
match('../foo/bar/somethingElse.js');
match('../foo/something-else.js');
This should be the regex for matching all dirs without filename.
/^(.*[/])[^/]+$/

jQuery parameters replace and upload between two strings in all URL's

WordPress' FacetWP plugin has a 'facetwp-loaded' jQuery event that allows for changes when facets are refreshed.
This is the 'facetwp-loaded' event's usage from FacetWP's documentation:
(function($) {
$(document).on('facetwp-loaded', function() {
// Changes go here
});
})(jQuery);
Facets produce URL's like:
http://website.com/hotels?fwp_location=worldwide
or
http://website.com/hotels/worldwide?fwp_location=europe
So I would like to make a global Regex redirection to substitute what is between
hotels
and
=
with
/
In the above examples, that would result in:
http://website.com/hotels/worldwide
or
http://website.com/hotels/europe
Can someone help me with this?
Thanks in advance
UPDATE
I've tried different Regex methods, but it seems to need jQuery parameter replace/update.
I don't have a way to test this using the Wordpress regex engine, so you'll have to check it, but it works in the R regex engine. Hopefully Wordpress supports perl style regex expressions.
Regex: Match: (?<=hotels).*?= and replace with /
In this case the piece of the string we want to remove is preceded by "hotels" and ends with an equal sign. So we want to match everything immediately after hotels, ending at the equal sign. To start matching immediately after "hotels" but not include it, we need to look backwards. So we use a look behind before the match. (?<=hotels) means look backwards from the current position in the string, and see if "hotels" precedes the current position. So when the engine gets to the "/" after hotels, it looks back and sees hotels (but it doesn't match, because it's a look behind). . matches any character, * means match zero or more (so zero or more of any character), and ? modifies the * telling the star to match zero or more characters, but only until the next character can be matched, in this case =.

javascript regexp to match path depth

Been struggling for the last hour to try and get this regexp to work but cannot seem to crack it.
It must be a regexp and I cannot use split etc as it is part of a bigger regexp that searches for numerous other strings using .test().
(public\/css.*[!\/]?)
public/css/somefile.css
public/css/somepath/somefile.css
public/css/somepath/anotherpath/somefile.css
Here I am trying to look for path starting with public/css followed by any character except for another forward slash.
so "public/css/somefile.css" should match but the other 2 should not.
A better solution may be to somehow specify the number of levels to match after the prefix using something like
(public\/css\/{1,2}.*)
but I can't seem to figure that out either, some help with this would be appreciated.
edit
No idea why this question has been marked down twice, I have clearly stated the requirement with sample code and test cases and also attempted to solve the issue, why is it being marked down ?
You can use this regex:
/^(public\/css\/[^\/]*?)$/gm
^ : Starts with
[^/] : Not /
*?: Any Characters
$: Ends with
g: Global Flag
m: Multi-line Flag
Something like this?
/public\/css\/[^\/]+$/
This will match
public/css/[Any characters except for /]$
$ is matching the end of the string in regex.

Match a specific sequence or everything else with regex

Been trying to come up with a regex in JS that could split user input like :
"Hi{user,10,default} {foo,10,bar} Hello"
into:
["Hi","{user,10,default} ","{foo,10,bar} ","Hello"]
So far i achieved to split these strings with ({.+?,(?:.+?){2}})|([\w\d\s]+) but the second capturing group is too exclusive, as I want every character to be matched in this group. Tried (.+?) but of course it fails...
Ideas fellow regex gurus?
Here's the regex I came up with:
(:?[^\{])+|(:?\{.+?\})
Like the one above, it includes that space as a match.
Use this:
"Hi{user,10,default} {foo,10,bar} Hello".split(/(\{.*?\})/)
And you will get this
["Hi", "{user,10,default}", " ", "{foo,10,bar}", " Hello"]
Note: {.*?}. The question mark here ('?') stops at fist match of '}'.
Beeing no JavaScript expert, I would suggest the following:
get all positive matches using ({[^},]*,[^},]*,[^},]*?})
remove all positive matches from the original string
split up the remaining string
Allthough, this might get tricky if you need the resulting values in order.

Categories