JS regex get text between characters [duplicate] - javascript

This question already has answers here:
How can I match a pipe character followed by whitespace and another pipe?
(5 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
I'm using this JS regex text.match(/|(https:.+?path.+?)|/)[1] to get a regex of a URL that is in between pipe | characters but it's not working.
The text is ||https://url.com/path/value|| but I can't seem to extract the URL from it. I need to have path in the middle to identify this particular URL since there are other URLs in the file.
It doesn't have to be a URL that I'm extracting. I mainly would like to know how to extract something from between a pair of characters (| in this case).

You need to escape the pipe ("|") characters:
text.match(/\|(https:.+?path.+?)\|/)[1]
Pipe is a special character that basically means "or". https://www.regular-expressions.info/alternation.html

To grab everything between the two sets of || then you could use this regex:
text.match(/\|\|(.*)\|\|/)
The first part \|\| matches the characters || literally.
The next part (.*)matches any character zero or more and groups the result.
The last part \|\| matches the closing characters || literally.

Related

Removing all occurrences of '^' from a String [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
I have this sorta string = "^My Name Is Robert.^"
I want to remove the occurrences of ^ from this string. I tried the replace method like :
replyText.replace(/^/g, '');
But it hasn't any affect. Using the replace without the global works but only removes the first occurrence.
Should I just make a loop and keep looping the string with replace till no more '^' are contained, or is there a better way?
You need to escape the ^ character in RegEx:
replyText.replace(/\^/g, '');
The caret, ^, is a special character in Regex, therefore it has to be escaped with a backslash i.e
replyText.replace(/\^/g, '')

Non-greed regex misunderstanding, /\/.*?$/ act like greed [duplicate]

This question already has answers here:
Regex lazy vs greedy confusion
(2 answers)
Why does a simple .*? non-greedy regex greedily include additional characters before a match?
(3 answers)
Closed 3 years ago.
I'm trying this in javascript
/\/.*?$/.exec('foo/bar/tar')[0]
I was expecting to get /tar as result but getting /bar/tar. As far as I understand non-greed regex would take the smallest match.
I'm circumventing this with myvar.split('/').reverse()[0] but I couldn't understand what is going wrong with the regex.
There is nothing wrong with the regex but the pattern \/.*?$ matches from the first forward slash until the end of the string non greedy.
The dot matches any character except a newline and does not take a forward slash into account, so that will result in /bar/tar.
If you want to match /tar, you could match a forward slash, followed by not matching anymore forward slashes using a negated character class and then assert the end of the string.
\/[^\/]+$
Pattern demo
console.log(/\/[^\/]+$/.exec('foo/bar/tar')[0]);

Why does this regex not exclude hyphens or brackets? [duplicate]

This question already has answers here:
Why this javascript regex doesn't work?
(1 answer)
Match exact string
(3 answers)
Closed 5 years ago.
Regex is the bane of my existence. I've done plenty tutorials, but the rules never stick, and when I look them up they seem to conflict. Anyways enough of my whining. Could someone tell me why this regex doesn't exclude hyphens or brackets:
/^[A-Za-z_][A-Za-z\d_]*/
The way I understand it (or at least what I'm trying to do), the ^ character dictates that the regex should start with the next thing on the list That means the regex should start with [A-Za-z_] or any character a-z and A-Z as well as and underscore _. Then the string can have anything that includes [A-Za-z\d_] which is any alphanumeric character and an underscore. Then I use the * to say that the string can have any number of what was presented previously (any alphanumeric character plus underscore). At no point to I specify a bracket [ or a hyphen -. Why does this expression not exclude these characters
Extra info
I'm verifying this with javascript:
function variableName(name) {
const reg = RegExp("^[A-Za-z_][A-Za-z\d_]*")
return reg.test(name)
}
function variableName("va[riable0") // returns true should be false
It's actually matching the first 2 letters("va"), that's why it's true.
To match the whole phrase, your reg expression should have "$" at the end:
"^[A-Za-z_][A-Za-z\d_]*$"
Your regex matches the part of the string that does not contain the bracket, because your're missing the $ anchor that would (together with ^) force it to match the whole string. Use
const reg = /^[A-Za-z_][A-Za-z\d_]*$/g
// ^
function variableName(name) {
return reg.test(name)
}
console.log(variableName("va[riable0"))

RegEx: non-consecutive special characters only allowed in the middle [duplicate]

This question already has answers here:
Regex to find not start and end with dot and allow some special character only not all
(3 answers)
Closed 2 years ago.
I am using following
ng-pattern="/^[a-zA-Z][a-zA-Z0-9._](.*[a-zA-Z0-9])?$/"
The matching String should
not start with a special character,
not end with special character, and
not include consecutive symbols except . (dot) and _ (underscore).
But it is not working.
Please, any suggestion.
Try using the word character class as a start ([\w] = [a-zA-Z0-9_]):
I'm not sure what you mean by consecutive symbols. But this might help:
/^[a-zA-Z]([\w.]*[a-zA-Z0-9])?$/
Maybe, have a look at the JavaScript RegExp Reference

Regex pattern to match this string [duplicate]

This question already has answers here:
regex pattern to match a type of strings
(4 answers)
Closed 8 years ago.
I need to match the below type of strings using a regex pattern in javascript.
E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>
So this single pattern should match both these strings:
1. /this/is/single-word
2. /this/is more-than/single/word-patterns/to-be-matched
Only the slash (/)and the 'this' in the beginning are consistent and contains only alphabets.
Try this -
^\/this(?:\/[\w\- ]+)+$
Demo here
There are some inconsistencies in your question, and it's not quite clear exactly what you want to match.
That being said, the following regex will provide a loose starting point for the exact strings that you want.
/this/(?:[\w|-]+/?){1,10}
This assumes the ' ' in your url was not intentional. This example will match a url with '/this/' + 1 to 10 additional '/' chunks.
(?:) -> non-matching group
[\w|-]+ -> one or more word characters or a hyphen
/? -> zero or one slashes
{1,10} -> 1 to 10 of the previous element, the non-matching group

Categories

Resources