Matching a specific string between foward slash or # using regex - javascript

I'm trying to make this regex:
([?=section\/en\/]*)([^\/#]*)
For these examples:
https://www.test.com/en/string-to-get#cid=4949
https://www.test.com/en/section/string-to-get/page&2#cid=4949
https://www.test.com/en/section/string-to-get#cid=4949
current regex

You need to use
(?<=section\/)([^\/#]*)
Or, just
section\/([^\/#]*)
and grab Group 1 value.
Here,
(?<=section\/) - a positive lookbehind that matches a location immediately preceded with section/ substring
([^\/#]*) - Capturing group 1: zero or more chars other than / and #.
See the regex demo #1 and regex demo #2.
Depending on whether or not regex delimiters are required and if they are not /s you may use an unescaped /, (?<=section/)([^/#]*) and section/([^/#]*).

Related

What will be the RegEx one or more, Which can validate all WebSocket Urls ? is there any npm module for the same? [duplicate]

I'm trying to write a regex for valid websocket address but I couldn't figure out what's wrong. Here's what I've already tried
/(^ws:\/\/|^wss:\/\/)(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:)|([a-zA-Z]+))([0-9]{1,5})/gm
Regex should match below
ws://0.0.0.0:8080
wss://192.168.0.25:12345
ws://localhost:3333
You may use
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[a-zA-Z]+):([0-9]{1,5})$/
See the regex demo
Note that [a-zA-Z]+ won't be enough as it only matches ASCII letters.
Use [^\/]+ instead if you want to match any chars but /:
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[^\/]+):([0-9]{1,5})$/
See this regex demo.
To just match FQDNs, use
/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|(?=[^\/]{1,254}:[0-9]{1,5}$)(?:(?=[a-zA-Z0-9-]{1,63}\.)(?:xn--+)?[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,63}):([0-9]{1,5})$/
Adaped from this solution by Tim Pietzcker. There are all the explanations in his post. See this regex demo.
The main problem was with :: it was only matched after numeric IP pattern, not after the [a-zA-Z]+.
Details
^ - start of string
(wss?:\/\/) - ws:// or wss://
([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[a-zA-Z]+) - 1 to 3 digits followed with 3 occurrences of . and 1 to 3 digits or 1+ letters (or, if [^\/]+ is used, 1+ chars other than /)
: - :
([0-9]{1,5}) - 1 to 5 digits
$ - end of string.
The second alternation does not match the :
You could move it outside of the alternation to match it for both of the options.
If you don't need the capturing groups, you could omit them.
The first alternation could be shortened making the extra s optional:
^(wss?:\/\/)(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|([a-zA-Z]+)):[0-9]{1,5}$
Regex demo

How to not match given prefix in RegEx without negative lookbehind?

Goal
The goal is matching a string in JavaScript without certain delimiters, i.e. a string between two characters (the characters can be included in the match).
For example, this string should be fully matched: $ test string $. This can appear anywhere in a string. That would be trivial, however, we want to allow escaping the syntax, e.g. The price is 5\$ to 10\$.
Summarized:
Match any string that is enclosed by two $ signs.
Do not match it if the dollar signs are escaped using \$.
Solution using negative lookbehind
A solution that achieves this goal perfectly is: (?<!\\)\$(.*?)(?<!\\)\$.
Problem
This solution uses negative lookbehind, which is not supported on Safari. How can the same matches be achieved without using negative lookbehind (i.e. on Safari)?
A solution that partially works is (?<!\\)\$(.*?)(?<!\\)\$. However, this will also match the character in front of the $ sign if it is not a \.
You might rule out what you don't want by matching it, and capture what you want to keep in group 1
\\\$.*?\$|\$.*?\\\$|(\$.*?\$)
Regex demo
You may use this regex and grab your inner text using capture group #1 as you are already doing in your current regex using lookbehind:
(?:^|[^\\])\$((?:\\.|[^$])*)\$
RegEx Demo
RegEx Details:
(?:^|[^\\]): Match start position or a non-backslash character in a non-capturing group
\$: Match starting $
(: Start capturing group
(?:\\.|[^$])*: Match any escaped character or a non-$ character. Repeat this group 0 or more times
): End capturing group
\$: Match closing $
PS: This regex will give same matches as your current regex: (?<!\\)\$(.*?)(?<!\\)\$

regEx match url config parameters

I need a regEx to identify custom parameters.
So if I have the url config path:
'/myapp/users/:userId'
it would match [':userId']
or
'/myapp/user/:username/profile/:profileId'
I need to return [':username',':profileId']
So far I have :(.*)/? but it selects everything after the initial found parameter
http://www.regextester.com/?fam=97974
I'm weak on reg ex, can anyone help please?
The :(.*)/? pattern matches the first :, then grabs the whole line greedily with .* and does not have to do anything else but return the match since /? matches an empty string (/? matches 1 or 0 / symbols).
You may use a negated character class [^\/]+:
:([^\/]+)
Details:
: - a colon
([^\/]+) - 1+ chars other than /
See the regex demo.
Try this pattern /:(.*?)($|\/)/g
Demo
Alternative $ asserts position at the end of the string
Alternative \/ matches the character
.*? matches any character (except for line terminators)

Grab full regex word if pattern inside it matches

How do I retrieve an entire word that has a specific portion of it that matches a regex?
For example, I have the below text.
Using ^.[\.\?\!:;,]{2,} , I match the first 3, but not the last. The last should be matched as well, but $ doesn't seem to produce anything.
a!!!!!!
n.......
c..,;,;,,
huhuhu..
I want to get all strings that have an occurrence of certain characters equal to or more than twice. I produced the aforementioned regex, but on Rubular it only matches the characters themselves, not the entire string. Using ^ and $
I've read a few stackoverflow posts similar, but not quite what I'm looking for.
Change your regex to:
/^.*[.?!:;,]{2,}/gm
i.e. match 0 more character before 2 of those special characters.
RegEx Demo
If I understand well you are trying to match an entire string that contains at least the same punctuation character two times:
^.*?([.?!:;,])\1.*
Note: if your string has newline characters, change .* to [\s\S]*
The trick is here:
([.?!:;,]) # captures the punct character in group 1
\1 # refers to the character captured in group 1

Regex match string until whitespace Javascript

I want to be able to match the following examples:
www.example.com
http://example.com
https://example.com
I have the following regex which does NOT match www. but will match http:// https://. I need to match any prefix in the examples above and up until the next white space thus the entire URL.
var regx = ((\s))(http?:\/\/)|(https?:\/\/)|(www\.)(?=\s{1});
Lets say I have a string that looks like the following:
I have found a lot of help off www.stackoverflow.com and the people on there!
I want to run the matching on that string and get
www.stackoverflow.com
Thanks!
You can try
(?:www|https?)[^\s]+
Here is online demo
sample code:
var str="I have found a lot of help off www.stackoverflow.com and the people on there!";
var found=str.match(/(?:www|https?)[^\s]+/gi);
alert(found);
Pattern explanation:
(?: group, but do not capture:
www 'www'
| OR
http 'http'
s? 's' (optional)
) end of grouping
[^\s]+ any character except: whitespace
(\n, \r, \t, \f, and " ") (1 or more times)
You have an error in your regex.
Use this:
((\s))(http?:\/\/)|(https?:\/\/)|(www\.)(?!\s{1})
^--- Change to negative lookaround
Btw, I think you can use:
(?:(http?:\/\/)|(https?:\/\/)|(www\.))(?!\s{1})
MATCH 1
3. [0-4] `www.`
MATCH 2
1. [16-23] `http://`
MATCH 3
2. [35-43] `https://`
Not quite sure what you're trying to do, but this should match any group of non-space characters not immediately preceded with "www." case insensitive.
/(https?:\/\/)?(?<!(www\.))[^\s]*/i
... [edit] but you did want to match www.
/(https?:\/\/)?([^\s\.]{2,}\.?)+/i
First things first, to match any whitespace char, use \S construct (in POSIX, you would use [^[:space:]], but JavaScript regex is not POSIX compliant). Here are some common patterns with \S:
\S* - zero or more non-whitespace chars
\S+ - one or more non-whitespace chars
Matching any text until first whitespace can mean match any zero or more chars other than whitespace, so, the answer to the current OP problem is
(?:www|https?)\S*
// ^^^
See the regex demo. This pattern will match up to the first whitespace or end of string. If there must be a whitespace char on the right use
(?:www|https?)\S*(?=\s)
The (?=\s) positive lookahead requires a whitespace immediately to the right of the current location.
Whenver there is a need to match until last whitespace you could match any zero or more chars that are followed with a whitespace, \s, pattern:
/(?:www|https?)[\w\W]*(?=\s)/
/(?:www|https?)[^]*(?=\s)/
// Or even (for ECMAScript 2018+):
/(?:www|https?).*(?=\s)/s
The [\w\W], [^] and . with s flag match any char including line break chars.

Categories