Simple Regex question - javascript

I want to remove this from a url string
http://.....?page=1
I know this doesn't work, but I was wondering how you would do this properly.
document.URL.replace("?page=[0-9]", "")
Thanks

It seems like you want to get rid of the protocol and the querystring. So how about just concatenating the remaining parts?
var loc = window.location;
var str = loc.host + loc.pathname + loc.hash;
http://jsfiddle.net/9Ng3Z/
I'm not entirely certain what the requirements are, but this fairly simple regex works.
loc.replace(/https?\:\/\/([^?]+)(\?|$)/,'$1');
It may be a naive implementation, but give it a try and see if it fits your need.
http://jsfiddle.net/9Ng3Z/1/

? is a regex special character. You need to escape it for a literal ?. Also use regular expression literals.
document.URL.replace(/\?page=[0-9]/, "")

The answer from #patrick dw is most practical but if you're really curious about a regular expression solution then here is what I would do:
var trimUrl = function(s) {
var r=/^http:\/\/(.*?)\?page=\d+.*$/, m=(""+s).match(r);
return (m) ? m[1] : s;
}
trimUrl('http://foo.com/?page=123'); // => "foo.com/"
trimUrl('http://foo.com:8080/bar/?page=123'); // => "foo.com:8080/bar/"
trimUrl('foobar'); // => "foobar"

You're super close. To grab the URL use location.href and make sure to escape the question mark.
var URL = location.href.replace("\?page=[0-9]", "");
location.href = URL; // and redirect if that's what you intend to do
You can also strip all query string parameters:
var URL = location.href.replace("\?.*", "");

Related

Javascript replace one or another

I'm trying to make the replace function work when having one match or another. It's very simple as a logic so I'd like to have a very simple implementation.
I have tried:
var my_url = document.URL;
var tmpl = "?tmpl=component" || "&tmpl=component"; //This is the tricky part
location.href = my_url.replace(tmpl,"");
...but it doesn't seem to work. Any ideas please?
This is not how JavaScript works, logical OR is useless here. One possible way is using regex:
location.href = my_url.replace(/[?&]tmpl=component/, "");
Here the replace method will replace any match of tmpl=component starting with either ? or &.
You're setting tmpl to be the value of the expression "?tmpl=component" || "&tmpl=component";, which will always evaluate to "?tmpl=component", since it is the first truthy value in your or statement.
You can do this with regex in a number of ways:
my_url.replace(/?tmpl=component|&tmpl=component/, "");
my_url.replace(/[?&]tmpl=component/, "");
You could do two replacements:
location.href = my_url.replace("?tmpl=component", "").replace("&tmpl=component", "");
or you could use a regular expression: (recommended)
location.href = my_url.replace(/[?&]tmpl=component/, "");
[?&] will match either a '?' or '&' character.
Best one is:
var tmpl = (my_url.indexOf("?tmpl=component") > -1)? "?tmpl=component" : "&tmpl=component";

Regex to detect a string that contains a URL or file extension

I'm trying to create a small script that detects whether the string input is either:
1) a URL (which will hold a filename): 'http://ajax.googleapis.com/html5shiv.js'
2) just a filename: 'html5shiv.js'
So far I've found this but I think it just checks the URL and file extension. Is there an easy way to make it so it uses an 'or' check? I'm not very experienced with RegExp.
var myRegExp = /[^\\]*\.(\w+)$/i;
Thank you in advance.
How bout this regex?
(\.js)$
it checks the end of the line if it has a .js on it.
$ denotes end of line.
tested here.
Basically, to use 'OR' in regex, simply use the 'pipe' delimiter.
(aaa|bbb)
will match
aaa
or
bbb
For regex to match a url, I'd suggest the following:
\w+://[\w\._~:/?#\[\]#!$&'()*+,;=%]*
This is based on the allowed character set for a url.
For the file, what's your definition of a filename?
If you want to search for strings, that match "(at least) one to many non-fullstop characters, followed by a fullstop, followed by (at least) one to many non-fullstop characters", I'd suggest the following regex:
[^\.]+\.[^\.]+
And altogether:
(\w+://[\w\._~:/?#\[\]#!$&'()*+,;=%]*|[^\.]+\.[^\.]+)
Here's an example of working (in javascript): jsfiddle
You can test it out regex online here: http://gskinner.com/RegExr/
If it is for the purpose of flow control you can do the following:
var test = "http://ajax.googleapis.com/html5shiv.js";
// to recognize http & https
var regex = /^https?:\/\/.*/i;
var result = regex.exec(test);
if (result == null){
// no URL found code
} else {
// URL found code
}
For the purpose of capturing the file name you could use:
var test = "http://ajax.googleapis.com/html5shiv.js";
var regex = /(\w+\.\w+)$/i;
var filename = regex.exec(test);
Yes, you can use the alternation operator |. Be careful, though, because its priority is very low. Lower than sequencing. You will need to write things like /(cat)|(dog)/.
It's very hard to understand what you exactly want with so few use/test cases, but
(http://[a-zA-Z0-9\./]+)|([a-zA-Z0-9\.]+)
should give you a starting point.
If it's a URL, strip it down to the last part and treat it the same way as "just a filename".
function isFile(fileOrUrl) {
// This will return everything after the last '/'; if there's
// no forward slash in the string, the unmodified string is used
var filename = fileOrUrl.split('/').pop();
return (/.+\..+/).test(filename);
}
Try this:
var ajx = 'http://ajax.googleapis.com/html5shiv.js';
function isURL(str){
return /((\/\w+)|(^\w+))\.\w{2,}$/.test(str);
}
console.log(isURL(ajx));
Have a look at this (requires no regex at all):
var filename = string.indexOf('/') == -1
? string
: string.split('/').slice(-1)[0];
Here is the program!
<script>
var url="Home/this/example/file.js";
var condition=0;
var result="";
for(var i=url.length; i>0 && condition<2 ;i--)
{
if(url[i]!="/" && url[i]!="."){result= (condition==1)? (url[i]+result):(result);}
else{condition++;}
}
document.write(result);
</script>

Javascript Regex with Match

I am trying to extract a number from a string with regular expression as I am told this would be the best approach for what I am wanting to do.
Here is the string:
http://domain.com/uploads/2011/09/1142_GF-757-S-white.jpg&h=208&w=347&zc=1&q=90&a=c&s=&f=&cc=&ct=
and I am trying to extract 208 from (height) from the string. so I know I have to look for "&h=" in the expression but I don't know what to do after that. How can I match between that and the next "&" but not include them as well...
Thanks..
Regular expression to match an h url parameter containing an integer value.
[&?]h=(\d+)
The Javascript:
var match = /[&?]h=(\d+)/.exec(url_string);
alert(match[1]);
Learn more about Regular Expressions.
To get the entire h=xxxx parameter, you can use this generic function (which you can reuse elsewhere for other purposes) and pass it the desired key:
function getParameterFromURL(url, key) {
var re = new RegExp("[&?]" + key + "=([^&]*)");
var matches = url.match(re);
if (matches) {
return(matches[1]);
}
return(null);
}
var url = "http://domain.com/uploads/2011/09/1142_GF-757-S-white.jpg&h=208&w=347&zc=1&q=90&a=c&s=&f=&cc=&ct=";
var parm = getParameterFromURL(url, "h");
See http://jsfiddle.net/jfriend00/86MEy/ for a working demo.

JS regex match (doesn't begin with [A-z]+://)www

I have some links in which people did not add the protocol to. I.e., www.stackoverflow.com. If the link begins with www., I want to replace it with 'http://www.'.
How can I do this with JavaScript regular expressions?
I tried the code below, but I can't seem to match the pattern 'doesn't start with [A-z]+://www.'.
The links are mixed in with text.
jQuery(document).ready(function () {
jQuery('.myClass').each(function (index) {
var temp = wwwify(jQuery(this).text());
jQuery(this).html(temp);
});
});
function wwwify(text) {
var regex = /(?!\b([A-z]+:\/\/))www\./igm;
return text.replace(regex, 'http://www.');
}
Why not just use the following?
if (text.substring(0,4)=='www.') {
text = 'http://'+text;
}
You could just easily replace each "http://www." to "www." and then replace all "www." to "http://www.". It might not be the prettiest regexp you could imagine, but it will solve your problem.
$(document).ready(function () {
$('.myClass').each(function (index) {
var $elm = $(this); // cache $(this) for reuse
var html = $elm.html();
html = html.replace(/http\:\/\/www\./ig, "www.").replace(/www\./ig, "http://www."); ;
$elm.html(html);
});
});
You need to anchor your regex to the start of the string. Also the range needs to be /[a-z]/ as the /i modifier will cover the upper-case possibilities. The /m and /g modifiers are irrelevant here. Leaving
var regex = /^(?![a-z]+:\/\/)www\./i;
My apologies, I missed the part saying "The links are mixed in with text". Without look-behind this can only be done using a function to return a replacement string. I suggest this, which captures any protocol before the www. and replaces it with http:// if it is blank
var regex = /\b([a-z]+:\/\/)?www\./ig;
text.replace(regex, function(url, protocol) {
return protocol ? url : "http://" + url;
});
Since I haven't found any suitable regex solutions through SO or elsewhere, just using regular javascript replace may be the best solution.
For now I'm making two passes through the text:
function wwwLineBeginsWith(text) {
var regex = /^www./gi;
return text.replace(regex, 'http://');
}
function wwwWordBeginsWith(text) {
var regex = /\swww./gi; return text.replace(regex, 'http://');
}
var test1 = 'www.test2.com';
test1 = wwwLineBeginsWith(test1);
test1 = wwwWordBeginsWith(test1);
console.log(wwwWordBeginsWith(test1));
How about replacing those with a protocol regardless?
function wwwify(text) {
return text.replace(/(http(s?):\/\/)?www\./ig, 'http$2://www.');
}
The reason it's currently not working is because JavaScript doesn't support lookbehinds, only lookaheads. You would need the syntax (?<!, which is not available in JavaScript regular expressions.
If you absolutely must use RegExp to determine this, I would recommend using something like /^[^Hh][^Tt]{2}[^Pp]:\/\// for the RegExp. Otherwise, I agree with the other posters... using indexOf would be far easier (i.e., url.toLowerCase().indexOf('http://') !== 0).

change domain portion of links with javascript or jquery

Sorry for my original question being unclear, hopefully by rewording I can better explain what I want to do.
Because of this I need a way to use JavaScript (or jQuery) to do the following:
determine domain of the current page being accessed
identify all the links on the page that use the domain www.domain1.com and replace with www.domain2.com
i.e. if the user is accessing www.domain2.com/index then:
Test 1
should be rewritten dynamically on load to
Test 1
Is it even possible to rewrite only a portion of the url in an href tag?
Your code will loop over all links on the page. Here's a version that only iterates over URLS that need to be replaced.
var linkRewriter = function(a, b) {
$('a[href*="' + a + '"]').each(function() {
$(this).attr('href', $(this).attr('href').replace(a, b));
});
};
linkRewriter('originalDomain.com', 'rewrittenDomain.com');
I figured out how to make this work.
<script type="text/javascript">
// link rewriter
$(document).ready (
function link_rewriter(){
var hostadd = location.host;
var vendor = '999.99.999.9';
var localaccess = 'somesite1.';
if (hostadd == vendor) {
$("a").each(function(){
var o = $(this);
var href = o.attr('href');
var newhref;
newhref = href.replace(/somesite1/i, "999.99.999.99");
o.attr('href',newhref);
});
}
}
);
</script>
You'll need to involve Java or something server-side to get the IP address. See this:
http://javascript.about.com/library/blip.htm
Replace urls domains using REGEX
This example will replace all urls using my-domain.com to my-other-domain (both are variables).
You can do dynamic regexs by combining string values and other regex expressions within a raw string template. Using String.raw will prevent javascript from escaping any character within your string values.
// Strings with some data
const domainStr = 'my-domain.com'
const newDomain = 'my-other-domain.com'
// Make sure your string is regex friendly
// This will replace dots for '\'.
const regexUrl = /\./gm;
const substr = `\\\.`;
const domain = domainStr.replace(regexUrl, substr);
// domain is a regex friendly string: 'my-domain\.com'
console.log('Regex expresion for domain', domain)
// HERE!!! You can 'assemble a complex regex using string pieces.
const re = new RegExp( String.raw `([\'|\"]https:\/\/)(${domain})(\S+[\'|\"])`, 'gm');
// now I'll use the regex expression groups to replace the domain
const domainSubst = `$1${newDomain}$3`;
// const page contains all the html text
const result = page.replace(re, domainSubst);
note: Don't forget to use regex101.com to create, test and export REGEX code.

Categories

Resources