JS get host character seek

Source: Internet
Author: User

 

Change the Application Path:

1 var href = location. href;
2 var RegEx = new Regexp ('^ http: // ([^/] +) *', 'G ');
3
4 var result;
5 if((result=regex.exe C (href ))! = NULL ){
6 href = Result [0];
7} else {
8 window. Alert ('host address not found! ');
9 return;
10}
11 href + = '/CSA ';
12 location. href = href;

Use a regular expression to obtain host escape: ^ http: // ([^/] + )*

 

The exec method of the regular expression:

If the match succeeds, the return value of this method is an array; otherwise, a null value is returned. In the returned array, the first value is a string that matches the regular expression, the second is the child seek that matches the first child seek matching the regular expression. as follows:

 

1 var href = 'HTTP: // www.163.com ';
2 var RegEx = new Regexp (': ^ http: // ([^/] +) *', 'G ');
3
4 var results;
5 If (Results = regex.exe C (href ))! = NULL ){
6 window. Alert (results [0]);
7 window. Alert (results [1]);
8}
9
10 // results [0] Is http://www.163.com
11 // results [1] is www.163.com

 

The detailed usage of exec () is as follows:

If exec () finds the matched text, an array of results is returned. Otherwise, null is returned. The first element of this array is the text that matches the regular expression, and the second element is the text that matches the 0th subexpression of regexpobject (if any ), the 2nd elements are texts that match the 2nd sub-expressions of regexpobject (if any), and so on. In addition to the array element and Length attribute, the exec () method returns two attributes. The index attribute declares the position that matches the first character of the text. The INPUT attribute stores the retrieved string. We can see that when calling the exec () method of a non-Global Regexp object, the returned array is the same as the array returned by calling the string. Match () method.

However, when regexpobject is a global regular expression, the behavior of exec () is slightly more complex. It will start searching string at the character specified by the lastindex attribute of regexpobject. When exec () finds the text that matches the expression, it sets the lastindex attribute of regexpobject to the next position that matches the last character of the text. This means that you can call the exec () method repeatedly to traverse all matched texts in the string. When exec () can no longer find the matching text, it returns NULL and resets the lastindex attribute to 0.

 

See http://www.w3school.com.cn/js/jsref_obj_regexp.asp

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.