www.webdeveloper.com
Results 1 to 5 of 5

Thread: Pattern matching for regular expression or metadata characters

  1. #1
    Join Date
    Oct 2013
    Posts
    3

    Smile Pattern matching for regular expression or metadata characters

    Hi there,

    I need to fix this issues.

    http://www.webmd.com/search/search_r...theadedness%20

    With this pattern matching expression [A-Za-z0-9_\-\+\.\~\(\)\=\;\'\,] in the program code. *I am unable to identify the above website link but I am not sure about how to include these patterns '?', '%' or a forward slash*to identify a web site of this kind.

    Could you please suggest me on this.*

    Thank you,
    Radhika*

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    242
    You have to escape "/" and "?" characters:

    Code:
    var regExp = /http:\/\/www.webmd.com\/search\/search_results\/default.aspx\?query=dizziness%20drowsiness%20lightheadedness%20/gi

  3. #3
    Join Date
    Oct 2013
    Posts
    3

    Thank you

    Thank you very much for the reply. I tried it before I could post here. It doesn't work with escape followed by "/" or "?" or "%".

    Quote Originally Posted by tech_soul8 View Post
    You have to escape "/" and "?" characters:

    Code:
    var regExp = /http:\/\/www.webmd.com\/search\/search_results\/default.aspx\?query=dizziness%20drowsiness%20lightheadedness%20/gi

  4. #4
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    242
    Are you trying to match a link which you have mentioned in previous post http://www.webmd.com/search/search_r...theadedness%20 or it is a part of some string?

    If it is a part of a string can you post a whole string?

    "%" character doesn't need to be escaped. "/" character needs to be escaped if you are using following regExp constructor.

    Code:
    var regExp = //
    If you don't escape "/" character in your regExp expression JavaScript will assume that you are closing regExp constructor instead of specifying forward slash character to match. Likewise you have to escape "?" character because it is a special RegExp character which says: match the previous item zero or one time.

    Some examples:

    Code:
    var string = "http://";
    var regExp = /\//g;
    var x = string.replace(regExp,"?");
    document.write(x)
    The output of the above code will be:

    Code:
    http:??
    Another example:

    Code:
    var string = "http://www.somewebsite.com/some%20webpage?some_other_page.html"
    var regExp = /%20|\?/g
    var x = string.replace(regExp,"_");
    document.write(x)
    The output of the above code will be:

    Code:
    http://www.somewebsite.com/some_webpage_some_other_page.htm
    And yet another expample:

    Code:
    var string = "http://www.somewebsite.com"
    var regExp = /(http)?:\/\/www.somewebsite.com/g
    var x = string.replace(regExp,"x");
    document.write(x)
    The output of the above code will be:

    Code:
    x
    Now if you change above string variable to hold this value:

    Code:
    var string = "ftp://www.somewebsite.com"
    The output will be:

    Code:
    ftpx
    Why? Because with

    Code:
    var regExp = var regExp = /(http)?:\/\/www.somewebsite.com/
    expression you told JavaScript to match the pattern with none or one occurrence of "http" followed by "://www.somewebsite.com". If you omit "?" character in "ftp" case there would be no pattern to match against.

    Hope now you understand why you have to escape "?" character or any other special character if it is a part of a string.
    Last edited by tech_soul8; 10-17-2013 at 09:07 AM.

  5. #5
    Join Date
    Oct 2013
    Posts
    3
    Thank you very much the explanation and example. It is helpful.







    Quote Originally Posted by tech_soul8 View Post
    Are you trying to match a link which you have mentioned in previous post http://www.webmd.com/search/search_r...theadedness%20 or it is a part of some string?

    If it is a part of a string can you post a whole string?

    "%" character doesn't need to be escaped. "/" character needs to be escaped if you are using following regExp constructor.

    Code:
    var regExp = //
    If you don't escape "/" character in your regExp expression JavaScript will assume that you are closing regExp constructor instead of specifying forward slash character to match. Likewise you have to escape "?" character because it is a special RegExp character which says: match the previous item zero or one time.

    Some examples:

    Code:
    var string = "http://";
    var regExp = /\//g;
    var x = string.replace(regExp,"?");
    document.write(x)
    The output of the above code will be:

    Code:
    http:??
    Another example:

    Code:
    var string = "http://www.somewebsite.com/some%20webpage?some_other_page.html"
    var regExp = /%20|\?/g
    var x = string.replace(regExp,"_");
    document.write(x)
    The output of the above code will be:

    Code:
    http://www.somewebsite.com/some_webpage_some_other_page.htm
    And yet another expample:

    Code:
    var string = "http://www.somewebsite.com"
    var regExp = /(http)?:\/\/www.somewebsite.com/g
    var x = string.replace(regExp,"x");
    document.write(x)
    The output of the above code will be:

    Code:
    x
    Now if you change above string variable to hold this value:

    Code:
    var string = "ftp://www.somewebsite.com"
    The output will be:

    Code:
    ftpx
    Why? Because with

    Code:
    var regExp = var regExp = /(http)?:\/\/www.somewebsite.com/
    expression you told JavaScript to match the pattern with none or one occurrence of "http" followed by "://www.somewebsite.com". If you omit "?" character in "ftp" case there would be no pattern to match against.

    Hope now you understand why you have to escape "?" character or any other special character if it is a part of a string.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles