This question already has answers here:
Regular Expression to find URLs in block of Text (Javascript)
(4 answers)
Closed last month.
Some possible sting like:
https://www.google.com
End: https://g.co
Inside https://b.co and https://c.co
How to match the URL anywhere in a string?
The URL may at the end of a string or end of space inside a string.
Currently I'm using the Regex like:
/(https:\/\/.*?)[$|\s]/gi
But it seems could not match the URL at the end of a string that the '$' does not work here.
Try this
/(https?:\/\/\S+)/gi
Or in your way
/(https:\/\/.*?)($|\s)/ig
To make things clear, anything inside "[]" already implies a "|". Adding it in "[]" will result in matching the character "|". Also $ inside the "[]" is considered as the char "$" and not as the end of string because of which your regex doesn't match the URL at the end of text.
Related
This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 2 years ago.
I am using the following regex to match URLs within text
new RegExp(`(https?://\\S+\\.\\S+)\\s`, 'ig');
The problem is when URL appear within brackets like this
(e.g. https://hello.com/fridaysforfuture-eu-socialmedia)
It picks off the closing bracket as part of the URL
My expected behavior is to include Closing bracket in the match if URL is like
https://hello.com/name-(1)
but exclude closing bracket if URL is within brackets like this
(e.g. https://hello.com/fridaysforfuture-eu-socialmedia)
Try to use :
RegExp("(^|[\s.:;?\-\]<\(])(https?://[-\w;/?:#&=+$\|\_.!~*\|'()\[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',\|\(\).:;?\-\[\]>\)])","i")
Reference: https://mathiasbynens.be/demo/url-regex
As far as I'm aware parenthesis are not valid characters to appear in an URL (at least not unencoded)
There are a bunch of RegEx-Patterns for different use-cases in this question:
What is the best regular expression to check if a string is a valid URL?
This question already has answers here:
Regex to match a string with specific start/end
(3 answers)
Closed 4 years ago.
I would like to find a string that starts and ends with a specific special character.
I tried the following regex but its not working:
(\#*\.|\&)[A-Za-z]+\.*#
I want to find any string that starts with #* and ends with *# but can't find the right regex for it.
Sample :
Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#
I am trying to find the string #*DCSN_RSN*# in the above string and replace it with <p>#*DCSN_RSN*#</p>
I think this is what you are looking for:
var sample = 'Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#';
var replaced = sample.replace(/(#\*[\s\S]*?\*#)/g, '<p>$1</p>');
console.log(replaced);
To match any characters you can use [\s\S]*, then add ? for [\s\S]*? to make it less-greedy. Your * characters also need to be escaped.
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.
This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 5 years ago.
I am trying to read a string formatted like
<test>input</test>\n <another>input</another>
My regex works for the test tagged input, but ignores the another tagged input. If I wrap the entire regex in parenthesis and use the brackets {} to specify how many times, then it only saves the last match case. How can I catch and save all match cases?
My regex:
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/
Result contents of match:
<test>input</test>\n <another>input</another>
<another>input</another>
another
input
/input
Add a g Modifier so specify that it is global (allows for multiple results)
So change your regexp to (notice the g in the end)
/([\n\s]*<([^>]+)>([^<>]*)<([^>]+)>[\n\s]*){0,}/g
This question already has an answer here:
Need to do a right trim on ajax query in javascript?
(1 answer)
Closed 8 years ago.
I wanted to check whether my string which ends with special character or not. If my string contains special character at the end, then need to trim at the right. if not, do nothing.
My piece of code:
var s = 'acbd#';
var x = 'abcd#e'
Expected Result:
acbd
abcd#e
any help on this?
You can use regular expression to replace the special characters with empty string, like this:
s.replace(/[#]+$/, "");
x.replace(/[#]+$/, "");
You can specify more special characters inside of square brackets.