How do I remove "http://" from a string in actionscript? - regex

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!

Related

Regular Expression for String without a "?" character to redirect to string with "?" character

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.

Regex for Case insensitve .net and Javascript

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"));

AS3 Regex test for URL

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\-\#?^=%&\/~\+#])?/;

Regular Expression for Google Analytics to determine page

I'm looking specifically for a regular expression that will grab the last term of a URL. This is not always a file name, it may not end in .html or .php, so I'll need to make sure that the regular expression is grabbing the last term from the URL.
Example:
I need to grab www.mydomain.com/anything_can_be_here/thankyoupage
I need to extract "thankyoupage" even when there can be any term preceding it in the URL.
Also note, there is no file extension on the thankyoupage URL segment.
This should do it:
/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/
For example, the result of this:
m = 'http://example.com/where/is?the=pancakes/house'.match(/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/);
is this array:
["http://example.com/where/is?the=pancakes/house", "is"]
And this:
m = 'http://example.com/where/is'.match(/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/)
Results in:
["http://example.com/where/is", "is"]
And this:
m = 'http://example.com/'.match(/^(?:http:\/\/)?(?:[^\/]+)\/.*?\/([^\/]+)(?:\?.*)?$/)
Results in null.
And your component is in m[1] and that comes from ([^\/]+). The (?:[^\/]+) will take care of the hostname (and the userinfo if it happens to be present), the (?:\?.*)?$ part will take care of any trailing CGI arguments.
Depending on your URLs, you could replace ^(?:http:\/\/)? with ^http:\/\/.
If you are only feeding it urls, something simple as .*/(.*) should work
that's assuming there is a '/' after the .com/.org/whatever
otherwise you'll get everything after the http://
what you need is the path name, which can be access using:
window.location.pathname;
Try this regex:
^http:\/\/.*/(.+)$
It will look for string starting with http:// then will go all the way till the last / and store everything after the last / into $1 variable.
The regexp:
/(\/([^\/]+))+/g
Take the 3rd element of the resulting array:
var a='http://www.host.com/aaa/bbb/ccc/dd.pp';
var regexp=/(\/([^\/]+))+/g;
var result=regexp.exec(a)
if( result.length==3) {
document.write('<p>'+result[2]+'</p>');
} else {
document.write('<p>Fail</p>');
}
Try this:
var str = "www.mydomain.com/other/other/this";
var path = /(?:https?:\/\/)?(?:www\.)?.*\/([^\/]+)/.exec(str)[1]; //this
Hope this is what you want
console.log(window.location.pathname.split('/').reverse()[0]);
Alright figured it outmyself, thanks anyways guys
/\/*\/thanks/
will match /thanks

Rewrite URL-string with String.replace in Actionscript 3

I'm getting a string that looks like this from a database: ~\Uploads\Tree.jpg
And I would like to change it in Actionscript3 to Uploads/Tree.jpg
Any idea how I can do this in neat way?
Assuming path is the string from the database, you can use this:
var newPath:String = path.replace(new RegExp("^~\\\\", "g"), "").replace(new RegExp("\\\\", "g"), "/")
If you always have the "~\" in the beginning, you can optimize it by using String.substring() instead. And if you are gonna convert many strings at once, make a reference to the regex and use that instead, so you do not create a new regex for each string.