regEx match url config parameters - javascript

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)

Related

Matching a specific string between foward slash or # using regex

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/([^/#]*).

Regex in JS: How to match a string that starts with "api" with exceptions

I need to match all string that starts with /api/v except those ends with /user/logout.
Ex:
/api/v2/segments (match)
/api/v2/user (match)
/api/v2/user/logout (NO match)
I'm trying with this regex but it doesn't work
/.*\/api\/v.*(^\/user\/logout)$/
You may use
/^\/api\/v(?!.*\/user\/logout$).*/
See the regex demo
Details
^ - start of string
\/api\/v - a /api/v string
(?!.*\/user\/logout$) - (a negative lookahead) no 0+ chars other than line break chars, as many as possible, followed with /user/logout at the end of string are allowed immediately to the right of the current location
.* - any 0+ chars other than line break chars as many as possible.
You need no .* at the end if you just test the string with RegExp#test for a match. If you want to use the match value, you need .* at the end.

Regex get entire word starting with #

basically im trying to create a regex pattern that get every word that starts with an #
For example :
#Server1:IP:Name Just a few words more
the pattern should find "#Server1:IP:Name"
Ive created a regex pattern that worked so far :
/#\w+/
The problem is everything after a colon wont get matched anymore. If i use this regex i get this as a result for example :
#Server1
how do i make sure it will get the entire word starting with an # and ignoring colons in it?
it is works fine try it:
#\w\S+
https://regex101.com/
\w matches any word character (equal to [a-zA-Z0-9_])
matches any non-whitespace character
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
You can use this
#[\w\s:]+
\w matches any word character (equal to [a-zA-Z0-9_])
\s matches any whitespace character (equal to [\r\n\t\f\v ])
: matches the character : literally (case sensitive)
If your string contains any (!##$%^&*()_+.) you could add them too.
try this #\S+
It gives you everything between "#" and the next space.
\S matches any non-whitespace character.
refer this

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.

Javascript regex - no white space at beginning + allow space in the middle

I would like to have a regex which matches the string with NO whitespace(s) at the beginning. But the string containing whitespace(s) in the middle CAN match. So far i have tried below
[^-\s][a-zA-Z0-9-_\\s]+$
Debuggex Demo
Above is not allowing whitespace(s) at the beginning, but also not allowing in the middle/end of the string. Please help me.
In your 2nd character class, \\s will match \ and s, and not \s. Thus it doesn't matches a whitespace. You should use just \s there. Also, move the hyphen towards the end, else it will create unintentional range in character class:
^[^-\s][a-zA-Z0-9_\s-]+$
If you plan to match a string of any length (even an empty string) that matches your pattern and does not start with a whitespace, use (?!\s) right after ^:
/^(?!\s)[a-zA-Z0-9_\s-]*$/
^^^^^^
Or, bearing in mind that [A-Za-z0-9_] in JS regex is equal to \w:
/^(?!\s)[\w\s-]*$/
The (?!\s) is a negative lookahead that matches a location in string that is not immediately followed with a whitespace (matched with the \s pattern).
If you want to add more "forbidden" chars at the string start (it looks like you also disallow -) keep using the [\s-] character class in the lookahead:
/^(?![\s-])[\w\s-]*$/
To match at least 1 character, replace * with +:
/^(?![\s-])[\w\s-]+$/
See the regex demo. JS demo:
console.log(/^(?![\s-])[\w\s-]+$/.test("abc def 123 ___ -loc- "));
console.log(/^(?![\s-])[\w\s-]+$/.test(" abc def 123 ___ -loc- "));
You need to use this regex:
^[^-\s][\w\s-]+$
Use start anchor ^
No need to double escape \s
Also important is to use hyphen as the first OR last character in the character class.
\w is same as [a-zA-Z0-9_]
use \S at the beginning
^\S+[a-zA-Z0-9-_\\s]+$
This RegEx will allow neither white-space at the beginning nor at the end of. Your string/word and allows all the special characters.
^[^\s].+[^\s]$
This Regex also works Fine
^[^\s]+(\s+[^\s]+)*$
try this should work
[a-zA-Z0-9_]+.*$
/^[^.\s]/
try this instead it will not allow a user to enter character at first place
^ matches position just before the first character of the string
. matches a single character. Does not matter what character it is, except newline
\s is space
If your field for user name only accept letters and middle of space but not for begining and end
User name: /^[^\s][a-zA-Z\s]+[^\s]$/
If your field for user ID only accept letters,numbers and underscore and no spaces allow
user ID: /^[\w]+$/
If your field for password only accept letters,number and special character no spaces allow
Password: /^[\w##&]+$/
Note: \w content a-zA-Z, number, underscore (_) if you add more character, add you special character after \w.
You can compare with user ID and password field in password field im only add some special character (##&).
India public thoko like 😁
I suggest below regex for this,
^[^\s].*[^\s]$
You can try regex in here

Categories