I have a following Regex , which is used in many places (both clientside validation and serverside validation)
Client Side
var _timeFormat = "^(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})( [AM|PM]{2,2}))\W{0}$";
server side
public const string TIME = #"^(?-i:1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})([AM|PM]{2,2}))\W{0}$";
My problem is when the user types am or Am/ pm or Pm it should accept.
I have tried
var _timeFormat = "^(?-i:1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})( [AM|PM]{2,2}))\W{0}$";
it works in serverside but on javascript it doesn't work.
please suggest what should i use.
I don't want to use
var re = new RegExp(_timeFormat, 'i');
because i am using that in many places and cannot change in all places.
Thanks in advance
I simplified your regex a bit. It will work both client and server-side.
^(0?[1-9]|1[0-2]):([0-5]\d)\s?([aApP][mM])$
Debuggex Demo
Version without groups (if you don't use them and check only validity of input value):
^(?:0?[1-9]|1[0-2]):[0-5]\d\s?[aApP][mM]$
Debuggex Demo
Since the RegExp constructor returns a copy of a passed in RegExp if passed one, you could just make _timeFormat into a case insensitive RegExp and have it propagate;
var _timeFormat = /^(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})( [AM|PM]{2,2}))\W{0}$/i;
var re = new RegExp(_timeFormat); // re is now also case insensitive
One reason your client side regex is failing is because you have not escaped the \ in your string.
You need to either escape the backslash with a double backslash:
"\\s"
or use a regex literal:
/\s/
You should try this:
var _timeFormat = /^(1|01|2|02|3|03|4|04|5|05|6|06|7|07|8|08|9|09|10|11|12{1,2}):(([0-5]{1}[0-9]{1}\s{0,1})(am|pm))\W{0}$/i;
alert(_timeFormat.test("01:10AM"));
Related
On our website we occasionally experience an error where dynamic links aren't building correctly.
URLs like this
https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*
Should actually be this:
https://www.test.url.edu/collections/search?edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*
We want to create a regular expression to redirect
/collections/&edan_fq[]=
to
/collections/search?edan_fq[]=
But everything after "edan_fq[]=" can change dynamically--there are thousands of permutations of the string after that point.
Does anyone know how this would be done?
If you use \& without Global Flag in Regex it will give first match. I've used JavaScript, please check this.
var data = "https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*";
var regex = /\&/
data = data.replace(regex,"search?");
console.log(data);
Please check Substitution example in Regex101.
While getting URL from youtube, those links are as follows:
http://www.youtube.com/watch?v=oSyL8WMLSB8
http://www.youtube.com/watch?v=4ioaMXTsAv8
http://www.youtube.com/watch?v=ZYMR6Ex9REY
http://www.youtube.com/watch?v=ok_VQ8I7g6I
I need to parse it into as follows using regex in Vb.net
http://www.youtube.com/v/oSyL8WMLSB8
http://www.youtube.com/v/4ioaMXTsAv8
http://www.youtube.com/v/ZYMR6Ex9REY
http://www.youtube.com/v/ok_VQ8I7g6I
That means remove watch? and replace = with / after v.
Sorry I'm new to regex...
You could use the below regex,
^(http:\/\/(?:www\.)?(?:youtube)\.com\/)[^?]+\?(v)=(.*)$
And in the replacement part, put the below
$1$2/$3
DEMO
OR
You could use a lookbehind to match the characters which are just after to .com/ upto the first = symbol.
(?<=\.com\/).*?=
And in the replacement part, replace the matched characters with,
v/
DEMO
Regex is overkill for this, and you don't need to use one. A simple string replacement is entirely sufficient. In C# that looks like this:
string url1 = "http://www.youtube.com/watch?v=oSyL8WMLSB8";
string url2 = url1.Replace("/watch?v=", "/v/");
// url2 => "http://www.youtube.com/v/oSyL8WMLSB8"
and in vb.net it looks like this:
Dim url1 As String = "http://www.youtube.com/watch?v=oSyL8WMLSB8"
Dim url2 As String = url1.Replace("/watch?v=", "/v/")
' url2 => "http://www.youtube.com/v/oSyL8WMLSB8"
Replace
/watch\?v=/
with
"v/"
Example:
(source: gyazo.com)
View a live regex demo!
Edit:
That means remove watch? and replace = with / after v.
On that case, see the following regex replacement:
/watch?(v)=/
Replaces to:
"$1/"
Example:
(source: gyazo.com)
View a live regex demo!
In Google Apps Scripts, I'm trying to match a URL using RegExp using the following function.
function testRegex(){
var str = "href='https://sites.google.com/a/domain.com/image-store/images/Image1.jpg?attredirects=0'";
var regex = new RegExp('http[:a-zA-Z\.\/\-_]{0,100}Image1.jpg', 'gi');
str = str.replace(regex,"new_url");
Logger.log(str);
}
When I input the same regexp and string into the regular expression tester at http://www.regular-expressions.info/javascriptexample.html , it works. However, it doesn't work in Google Apps Scripts.
Any ideas why?
EDIT:
I figured the problem is with the underscore. Replacing with \w helps. So, when I replace the Regex with
https[\.a-zA-Z0-9\/+:\w-]{0,100}Image1.jpg
IT WORKS.
But, it still doesn't match an underscore. For example, it doesn't work with the following the URL
https://sites.google.com/a/domain.com/image-store/_/rsrc/1351707816362/images/Image1.jpg
Adding a + after the slash might do it:
function testRegex(){
var str = "href='https://sites.google.com/a/domain.com/image-store/images/Image1.jpg?attredirects=0'";
var regex = new RegExp('http[:a-zA-Z\.\/+\-_]{1,100}Image1.jpg', 'gi');
str = str.replace(regex,"new_url");
Logger.log(str);
}
I didn't debug your code, but I tried it on repl.it and verified that it is not correct in Chrome's V8 JavaScript either. I suspect there is a bug here that is unrelated to Apps Script.
EDIT: This works:
function testRegex(){
var str = "href='https://sites.google.com/a/domain.com/image-store/_/rsrc/1351707816362/images/Image1.jpg'";
var regex = new RegExp('https[\.a-zA-Z0-9\/+:\w_-]{0,100}Image1.jpg', 'gi');
str = str.replace(regex,"new_url");
Logger.log(str);
}
It didn't match underscores because you didn't specify an underscore in the character class.
I want to check if a string contains a URL.
var string:String = "Arbitrary amount of random text before and after.<br/><br/>http://www.somdomain.co.uk<br/><br/>Tel: 0123 456 789<br/>";
var pattern:RegExp = new RegExp("(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?");
ExternalInterface.call('console.log',pattern.test(string));
This outputs false to my console, whereas when I feed the regex and string into http://gskinner.com/RegExr/, the url is found.
What am I doing wrong and how do I fix it?
I just escaped a couple extra / and it started working. Also, I generally like to use the regex literal notation to create regexes as it gives you better syntax highlighting than converting a string to a regex in the new Regex():
var pattern:RegExp = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/;
This one may seem basic but I don't know how to do it - anybody else?
I have a string that looks like this:
private var url:String = "http://subdomain";
What regex do I need so I can do this:
url.replace(regex,"");
and wind up with this?
trace(url); // subdomain
Or is there an even better way to do it?
Try this:
url.replace("http:\/\/","");
Like bedwyr said. :)
This will match only at the beginning of the string and will catch https as well:
url.replace("^https?:\/\/","");
ActionScript does indeed support a much richer regex repetoire than bewdwyr concluded. You just need to use an actual Regexp, not a string, as the replacement parameter. :-)
var url:String;
url = "https://foo.bar.bz/asd/asdasd?asdasd.fd";
url = url.replace(/^https?:\/\//, "");
To make this perhaps even clearer
var url:String;
var pattern:RegExp = /^https?:\/\//;
url = "https://foo.bar.bz/asd/asdasd?asdasd.fd";
url = url.replace(pattern, "");
RegExp is a first class ActionScript type.
Note that you can also use the $ char for end-of-line and use ( ) to capture substrings for later reuse. Plenty of power there!