Some notes on software development... RSS 2.0
# Wednesday, 26 November 2014

Before using JavaScript’s RegExp object you may need to escape regular expression characters. These characters are: ( ) [ { * + . $ ^ \ | ?

I’m no RegEx expert but the code I found that works for me is:

text.replace(/([()[{*+.$^\\|?])/g, '\\$1');

 

Thanks once again StackOverflow :-)

Links
http://stackoverflow.com/questions/5663987/how-to-properly-escape-characters-in-regexp

Wednesday, 26 November 2014 10:47:48 (GMT Standard Time, UTC+00:00)  #    -
JavaScript | Regular Expressions
# Tuesday, 27 May 2008

If you save a HTML page to a text file, or as a string in memory, you are likely to get the relative path contained in any IMG or HREF tags

Example snippet:

<H1>Hello from Test Page</H1>
<IMG src="images/TestImage1.jpg">

When you then load you saved HTML page you will not see the image because the HTML is looking for TestImage1.jpg in the images folder which doesn't exist. All that exists is your saved HTML text file.
So we need to parse the HTML and prefix the missing server path to the src tag in the HTML.

The most efficient way to achieve this is to use the power of Regular Expressions, but I'm no expert with RegEx's so after trawling the Internet looking for a suitable RegEx example, rather than read a book ;), I finally found the correct expression at code.nontalk.com.

The pattern: "<(.*?)(src|href)=\"(?!http)(.*?)\"(.*?)>"
The Match Evaluator: "<$1$2=\"" + absoluteUrl + "$3\"$4>"

The example method:

public static String ConvertRelativePathsToAbsolute(String text, String absoluteUrl)
{
    String value = Regex.Replace(text, "<(.*?)(src|href)=\"(?!http)(.*?)\"(.*?)>", "<$1$2=\"" + absoluteUrl + "$3\"$4>",
                                 RegexOptions.IgnoreCase | RegexOptions.Multiline);

    // Now just make sure that there isn't a // because if
    // the original relative path started with a / then the
    // replacement above would create a //.

    return value.Replace(absoluteUrl + "/", absoluteUrl);
}

Using the method:


ConvertRelativePathsToAbsolute(myHTML, "http://localhost/")

Will return:

<H1>Hello from test Page</H1>
<IMG src="http://localhost/images/TestImage1.jpg">


Works great for me so thanks nontalk.com!


Links
code.nontalk.com - Convert Relative Paths to Absolute Using Regular Expressions

Tuesday, 27 May 2008 18:27:08 (GMT Standard Time, UTC+00:00)  #    -
C# | Regular Expressions
# Tuesday, 05 February 2008

If you need to match a single word from a range of words you can use regular expression Alternation. This syntax uses the pipe (|) to represent the logical OR for matching. Here is a RegEx that only matches on 1 of the 4 words (depending on case sensitivity):

^(word1|word2|word3|word4)$

If you need to also allow a possitive macth on an empty string you can and another pipe (|) with no word after like so:

^(word1|word2|word3|word4|)$

This will match a blank word or either of the words listed.

Tuesday, 05 February 2008 11:40:22 (GMT Standard Time, UTC+00:00)  #    -
Regular Expressions
Navigation

Sign In
Extras
Statistics
Total Posts: 116
This Year: 0
This Month: 0
This Week: 0
Comments: 36
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2017
Hadrian Phillips

All Content © 2017, Hadrian Phillips
DasBlog theme 'Business' created by Christoph De Baene (delarou)