JS get URL path includes regular match rule to get

Source: Internet
Author: User

General Get methods:

var protocol = Window.location.protocol; "http:"

var host = Window.location.host; "localhost" or "www.baidu.com"

var pathname = Window.location.pathname; "/dkrl/services/cases.php"

var href = window.location.href; "Http://localhost/dkrl/services/cases.php?noId=7"

var search = Window.location.search; "? noid=7"


Use JS to get the current page name


var strurl = window.location.href;

var arrurl = Strurl.split ("/");

var strpage = arrurl[arrurl.length-1];

var indexof = Strpage.indexof ("?");

if (indexof! =-1) {

Strpage = Strpage.substr (0,strpage.indexof ("?"));

}

alert (strpage);


Regular Fetch method:


Match any character (. *)


The substitution of JS strings and the use of the Replace () method


The Replace (Regexp,replacement) method has two parameters, the first parameter can be a plain text string or a RegExp object, see the use of RegExp object, the second parameter is a string can also be a function.


The following is an example of JS string substitution:


Example 1:

var str= "Hello world!";

document.write (Str.replace (/world/, "Phper"));


Example 2:

var reg=new RegExp ("(\\w+), (\\d+), (\\w+)", "GMI");

var info= "Lili,14,china";

var rep=info.replace (Reg, "She is $ $, $ years old, come from $");

Alert (rep);


JS get URL path includes regular match rule to get

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.