I have a URL as Follow:
http://example.com/category/news/page/2/
I need to replace any number that comes at the end of URL which represents page number.
If possible which I think it is, I want to use regular expression in case the domain changes, the code still works.
I am also using PHP ...
Could help me with a proper RegEx?
Find The Answer:
string.replace(/\/page\/[0-9]+/, '/page/' + pageNum);
pageNum can be any variable to replace the page number
Related
I'm trying to get a parameter stored in a html comment using regex. However when I execute the expression it return the widest string possible and not all the possible matches.
So I have some content that might include this string:
<!--url:/new--><!--title:My Title-->
I use the following simply expression to get the url I need:
/<!--url:(.*)-->/
The issue I have is that the result match part of the title which is of course valid but not what I was looking for
["<!--url:/new--><!--title:My Title-->", "/new--><!--title:My Title"]
There is workarounds I can use like making sure there is a line break after each parameter line but I prefer to have a solid regex and also of course understand what I missing out.
PS: Please comment if you come up with a better title.
Make the regex non-greedy:
/<!--url:(.*?)-->/
You can test this regex by clicking here:
Regex101
In Twitter, if you post a link in tweet, for example, http://stackoverflow.com/questions/8699459/get-title-content-via-link-in-rails
the URL automatically changes to a shorten one:
And the correspongding Html is:
<span class="invisible">http://</span>
<span class="js-display-url">stackoverflow.com/questions/8699</span>
<span class="invisible">459/get-title-content-via-link-in-rails</span>
(the http:// and long partis hidden)
So How can I achieve this? I believe it's something with JS? or How should I approach this with Rails?
Many Thanks!!!
I think this is what you're looking for or at least close to it.
aHref.innerHTML = aHref.match(/([\w\d\-_]*\.)+(\w*$|\w*\/[\w\d]*)/)[0];
In short; this ignores the protocol:// and takes every string ending in "." (for sub domains) until it reaches the last item (could be "com", could be "uk" as in "co.uk") denoted by either "$" (end of the string) or a "/" followed by an alphanumeric string. With the resulting string it replaces the innerHTML or displayed content value.
So for example: "http://test.couch.com/333/fire" would become "test.couch.com/333"
Edit: I should add that I've only accounted for
A-Za-z0-9 and -_
in the url up to the "/" and only
A-Za-z0-9
For the remainder.
Edit 2: Using in Rails controller
The same principle applies but without the innerHTML since it's a DOM element property
url = url.match(/([\w\d\-_]*\.)+(\w*$|\w*\/[\w\d]*)/).first
The line above will do the same thing as the javascript or at least will create the same shortened url.
If you mean how to use with the a helper:
shortUrl= url.match(/([\w\d\-_]*\.)+(\w*$|\w*\/[\w\d]*)/).first
link_to shortUrl, url
I am looking to detect last url from text using javascript or mootools. Url canbe without prefix/scheme
I am working on URL auto sense like Facebook. Where a user may give an URL www.example.com or with http://www.example.com either of them should be detected by JavaScript. see stackoverflow detected URL that included with scheme without URL scheme it couldn't detect URL. In my case I need both.
Here is some text
'http://www.example.com www.example2.com'
Now I want www.example2.com It will be better if I get full array containing both http://www.example.com and www.example2.com
I searched a lot but couldn't find solution.
Most close to my requirements were Question about URL Validation with Regex and How do I extract a URL from plain text using jQuery?
Any help greatly appreciated.
by combing info in these 2 links:
How do I extract a URL from plain text using jQuery?
Detect URLs in text with JavaScript
We can get this:
http://jsfiddle.net/qQwGA/1/
If I understand what you're trying to do, this should cover it.
Given your input string, I think you just want to split it using spaces as separator?
.split(' ') ?
REGEX
/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi
**SAMPLE CODE**
var str = 'http://www.example.com www.example2.com scheme://username:password#domain:port/path?query_string#fragment_id';
var t = str.match(/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi);
/*
t contains :
[
"http://www.example.com",
"www.example2.com",
"scheme://username:password#domain:port/path?query_string#fragment_id"
]
*/
**DEMO**
>http://jsfiddle.net/wvYTd/
**DISCUSSION**
This regex will find any substring that looks like an URL in an input string.
No validation is performed on any URL found. For instance, if the input string is 3aBadScheme://hostname, the regex will detect it as an URL. In this example, 3aBadScheme is invalid since a scheme MUST start with a letter.
Excerpt from RFC3986
(...)Scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-").(...)
I have to implement some type of pixel for analytic and it requires passing a session Id in the url string. My sessionID contains special characters. It looks something like this BFhGlzT6FBkDr2Zndp0!-1309
I need to remove the (-!) characters from this string, how do I achieve this using jquery? I need to make sure jquery remove those characters before it render otherwise it will not report a visit to analytic.
Guys thanks your help but maybe I need to explain bit further, my pixel code look some what like this "
img src="https://sometime.com/png?org_id=k8vif92&session_
id=T6PtTyRSqhGPYBhp84frwth67n6fL7wcLBFhGlzT6FBkDr2Zndp0!-130901808!1319637471144&m=2&m=2" alt="">
Before this pixel fire off, I need to replace the character in the sessionId string to remove !- and keep in mind session id will change every time there is a new session. I need a code that is generic so it works no matter what session id is, it needs to delete special characters from it.
Try using .replace:
var token = "BFhGlzT6FBkDr2Zndp0!-1309";
token.replace(/[^A-Za-z0-9]/g, "");
this will remove any character that's not a letter or number. More concisely:
token.replace(/\W/g, "");
(this won't replace underscores)
Black-listing ! and - (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[!\-]/g,""));
White-listing alpha-numeric (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[^a-z0-9]/ig,""));
var str = "BFhGlzT6FBkDr2Zndp0!-1309".replace("!-","");
fiddle: http://jsfiddle.net/neilheinrich/eYCjX/
Define a regular expression character set that contains all the allowed characters. For example, yours might look like /[a-zA-Z0-9]/. Now invert it with the complementary character set [^a-zA-Z0-9] and remove all those characters with the String.replace method.
mystring = mystring.replace(/[^a-zA-Z0-9]/g, "");
You dont need jquery for this. You can use javascript regex. See this jsfiddle
var code = "BFhGlzT6FBkDr2Zndp0!-1309";
code = code.replace(/[!-]/g,"");
alert(code);
I've got this regex pattern from WMD showdown.js file.
/<((https?|ftp|dict):[^'">\s]+)>/gi
and the code is:
text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi,"$1");
But when I set text to http://www.google.com, it does not anchor it, it returns the original text value as is (http://www.google.com).
P.S: I've tested it with RegexPal and it does not match.
Your code is searching for a url wrapped in <> like: <http://www.google.com>: RegexPal.
Just change it to /((https?|ftp|dict):[^'">\s]+)/gi if you don't want it to search for the <>: RegexPal
As long as you know your url's start with http:// or https:// or whatever you can use:
/((https?|s?ftp|dict|www)(://)?)[A-Za-z0-9.\-]+)/gi
The expression will match till it encounters a character not allowed in the URL i.e. is not A-Za-z\.\-. It will not however detect anything of the form google.com or anything that comes after the domain name like parameters or sub directory paths etc. If that is your requirement that you can simply choose to terminate the terminating condition as you have above in your regex.
I know it seems pointless but it may be useful if you want the display name to be something abbreviated rather than the whole url in case of complex urls.
You could use:
var re = /(http|https|ftp|dict)(:\/\/\S+?)(\.?\s|\.?$)/gi;
with:
el.innerHTML = el.innerHTML.replace(re, '<a href=\'$1$2\'>$1$2<\/a>$3');
to also match URLs at the end of sentences.
But you need to be very careful with this technique, make sure the content of the element is more or less plain text and not complex markup. Regular expressions are not meant for, nor are they good at, processing or parsing HTML.