Extract the filename from a URL
var regexp = /(\w|[-.])+$/ str = document.URL a = regexp.exec(str) alert(a[0])
Reference: Regular Expressions: Methods - Doc JavaScript [webreference.com]
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
var regexp = /(\w|[-.])+$/ str = document.URL a = regexp.exec(str) alert(a[0])
require 'cgi' # escape name = "ruby?" value = "yes" url = "http://example.com/?" + CGI.escape(name) + '=' + CGI.escape(value) + "&var=T" # url: http://example.com/?ruby%3F=yes&var=T html = %(<a href="#{CGI.escapeHTML(url)}">example</a>) # html: <a href="http://example.com/?ruby%3F=yes&var=T">example</a> # unescape name_encoded = html.match(/http:([^"]+)/)[0] # name_encoded: http://example.com/?ruby%3F=yes&var=T href = CGI.unescapeHTML(name_encoded) # href: http://example.com/?ruby%3F=yes&var=T query = href.match(/\?(.*)$/)[1] # query: ruby%3F=yes&var=T pairs = query.split('&') # pairs: ["ruby%3F=yes", "var=T"] name, value = pairs[0].split('=').map{|v| CGI.unescape(v)} # name, value: ["ruby?", "yes"]
#!/usr/bin/ruby class ShortUrl def initialize() @chars = ('a'..'z').to_a + ('1'..'9').to_a + ('A'..'Z').to_a @array_size = @chars.size @h = Hash.new @chars.each {|c| @h[c] = '0'} vowels = %w(a e i o u A E I O U 4 3 1 0) vowels.each {|v| @h[v] = '1'} nums = %w(2 5 6 7 8 9) nums.each {|n| @h[n] = '2'} @count = 0 @a = Array.new(7, -1) @k = 0 end def iterate_chars(array_size) (0..array_size).each {|i| increment_index(@k) convert_to_chars() } end def convert_to_chars() buffer = '' @a.each {|i| buffer << @chars[i] if i >= 0 } a = buffer.reverse.scan(/./) k = a.length if (k > 1) if ((@h[a[k-2]] + @h[a[k-1]]) != '10') puts buffer.reverse end else puts buffer.reverse end end def get_short_url(count) if count > @array_size new_count = count - @array_size iterate_chars(@array_size) get_short_url(new_count) else iterate_chars(count) end end def increment_a(i) if @a[i] < @array_size - 1 @a[i] = @a[i] + 1 return i else @a[i] = 0 return i += 1 end end def increment_index(k) old_k = k k = increment_a(k) if k != old_k increment_index(k) else k = 0 end k end end if __FILE__ == $0 su = ShortUrl.new su.get_short_url(222761) end
module ActionController::Routing class DynamicSegment def interpolation_chunk "\#{CGI.escape(#{local_name}.to_s)}" end def match_extraction(next_capture) default_value = default ? default.inspect : nil %[ value = if (m = match[#{next_capture}]) CGI.unescape(m) else #{default_value} end params[:#{key}] = value if value ] end end end
<file name="links.txt" location="/var/www/localhost/"> scotsman /gwd/feed/scotsman.html digg /gwd/feed/digg.html </file> <IfModule mod_rewrite.c> RewriteEngine on RewriteMap links txt:/var/www/localhost/links.txt RewriteRule ^/l/(.*) ${links:$1|http://mysite.com/} [R] </IfModule>
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^/shortcut$ /complicated/and/way/too/long/url/here </IfModule>
def create_callname (title) title.downcase.gsub(/[^a-z0-9]+/i, '-') end
validates_each :href, :on => :create do |record, attr, value| begin uri = URI.parse(value) if uri.class != URI::HTTP record.errors.add(attr, 'Only HTTP protocol addresses can be used') end rescue URI::InvalidURIError record.errors.add(attr, 'The format of the url is not valid.') end end
require 'net/http' require 'rexml/document' url = 'http://www.example.com/journal080907.xml' element_name = ARGV[0] # basic_category xpath = ARGV[1] # eg.'entries/entry' file = ARGV[2] # eg. 'journal080907.xml' element_value = ARGV[3] # 'ruby' file = File.new(file,'w') # get the XML data as a string xml_data = Net::HTTP.get_response(URI.parse(url)).body # extract event information doc = REXML::Document.new(xml_data) docx = doc node = docx.elements[xpath] element = node.elements[element_name] puts element.text element.text = 'ruby' file.puts docx
#include <windows.h> #include <httpfilt.h> #define MAX_URL_LEN 4096 BOOL WINAPI GetFilterVersion(HTTP_FILTER_VERSION * pVer) { pVer->dwFlags = (SF_NOTIFY_SECURE_PORT | SF_NOTIFY_NONSECURE_PORT | SF_NOTIFY_PREPROC_HEADERS | SF_NOTIFY_ORDER_HIGH); pVer->dwFilterVersion = HTTP_FILTER_REVISION; strcpy_s(pVer->lpszFilterDesc,9,"Blah blah blah"); return TRUE; } void ReMapURLs(CHAR* pUrl,HTTP_FILTER_CONTEXT* pfc,PHTTP_FILTER_PREPROC_HEADERS pHeaders) { if (pUrl[0] != '/') return; CHAR *iUrl = 0; BOOL doSet = FALSE; char *sOldUrls[] = { "/test/", "/TEST/" }; char *sNewUrls[] = { "/go/", "/GO/" }; for (int i=0; i<2; i++) { if (iUrl = strstr(pUrl,sOldUrls[i])) { doSet = TRUE; memcpy(iUrl,sNewUrls[i],strlen(sNewUrls[i])); } } if (doSet) pHeaders->SetHeader(pfc, "url", pUrl); } DWORD WINAPI HttpFilterProc(HTTP_FILTER_CONTEXT *pfc,DWORD NotificationType,VOID * pvData) { PHTTP_FILTER_PREPROC_HEADERS pHeaders; DWORD cUrlOrig = MAX_URL_LEN; DWORD cUrl = cUrlOrig; CHAR rgUrl[MAX_URL_LEN]; CHAR *pUrl; BOOL result; switch ( NotificationType ) { case SF_NOTIFY_PREPROC_HEADERS: pHeaders = (PHTTP_FILTER_PREPROC_HEADERS) pvData; result = pHeaders->GetHeader(pfc, "url", rgUrl, &cUrl); if (!result && cUrl > cUrlOrig) { pUrl = (CHAR*)LocalAlloc(0, cUrl); result = pHeaders->GetHeader(pfc, "url", pUrl, &cUrl); if (!result) { LocalFree(pUrl); break; } ReMapURLs(pUrl, pfc, pHeaders); LocalFree(pUrl); } else ReMapURLs(rgUrl, pfc, pHeaders); break; default: break; } return SF_STATUS_REQ_NEXT_NOTIFICATION; }