43439

How to write a regex expression that replace anchors and links value

Question:

My html code has many anchors and links such as :

<script src="/Common/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> <link href="/Common/Css/UserAdmin.css" rel="stylesheet" type="text/css" /> <a href="/test.php">test</a></li>

I want to change them into:

<script src="http://www.mydomain.com/Common/Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> <link href="http://www.mydomain.com/Common/Css/UserAdmin.css" rel="stylesheet" type="text/css" /> <a href="http://www.mydomain.com/test.php">test</a></li>

ie. add a http://www.mydomain.com before every src and href value, how to achieve this using regex expression. PS: I use PHP language.

Answer1:

Use the following regexp:

([\b]*)(src|href)([\b]*=[\b]*")(.+?")

and replace with:

\1\2\3http://www.mydomain.com\4

The main idea here is to use the lazy operator "+?"

Group1: ([\b]*) 0 or more blanks ... followed by Group2: (src|href) src or href ... followed by Group3: ([\b]*=[\b]*") 0 or more blanks followed by = followed by 0 or more blanks followed by a quote ... followed by Group4: (.+?") any group of characters until the nearest quote

then replace with what has matched Group1 then Group2 then Group3, insert <a href="http://www.mydomain.com" rel="nofollow">http://www.mydomain.com</a>, then what has matched Group4

Answer2:

Don't hurt yourself with regexp if you don't need to.

$search = array('<script src="/', '<link href="/', '<a href="/'); $d = 'http://www.mydomain.com'; $replace = array('<script src="'.$d.'/', '<link href="'.$d.'/', '<a href="'.$d.'/'); str_replace($search, $replace, $s)

Recommend

  • How to add fields in extended UserAdmin
  • nested attributes not saving the user_id in join table
  • Django inlinemodeladmin extra option not working
  • Flask Admin doesn't show all fields
  • Can't delete li from to-do list
  • Responsive design method for collapsing a div
  • using html data-attributes as css-variable (i.e. text-shadow)
  • Cut the background to expose the layer below
  • Outlines on links in IE9 remains when focus is changed
  • Center align outputs in ipython notebook
  • Simulate click Geckofx vb,net
  • How to view images from protected folder with php?
  • Adjust width of select element according to selected option's width
  • jQuery ready not fired after rails link_to is clicked
  • Webgrid not refreshing after delete MVC
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Jquery UI tool tip close icon
  • Using jQuery closest() method with class selector
  • HTML download movie download link
  • Updating server-side rendering client-side
  • Modifying destination and filename of gulp-svg-sprite
  • WinForms: two way TextBox problem
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • Jquery - Jquery Wysiwyg return html as a string
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • trying to dynamically update Highchart column chart but series undefined
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal