1

HI

I have an application where users can paste their embed code.While displaying I want to change the height and width of the embed code to a fixed value of my choice. Can you tell me the regular expression to find the width and height in the embed code. width="300" height="500". I want to be able to get these two values completely so that I can replace them.

Thanks

Luke Schafer
  • 9,209
  • 2
  • 28
  • 29

3 Answers3

1

The following examples take into account the ordering, what quotations are used, and if people put in spaces

A straight replace:

embeddedString = embeddedString.replace(/(width\s*=\s*["'])[0-9]+(["'])/ig, $1 + yourWidth + $2);
embeddedString = embeddedString.replace(/(height\s*=\s*["'])[0-9]+(["'])/ig, $1 + yourHeight + $2);

Or to transform the width:

embeddedString = embeddedString.replace(/width\s*=\s*["']["']/ig, function($0, $1)
{
    return $1 * 2;
});

if you actually want to remove the whole thing, but use the values:

var originalWidth;
var originalHeight;
embeddedString = embeddedString.replace(/(?:width|height)\s*=\s*["']([0-9]+)["']\s*(?:width|height)\s*=\s*["']([0-9]+)["']/ig, function($0, $1, $2, $3, $4)
{
    // $0 = original string
    // $1 = either 'width' or 'height'
    // $2 = the value of width or height, depending on above
    // $3 = either 'width' or 'height'
    // $4 = the value of width or height, depending on above
    // here you might want to set another value, eg:
    originalWidth = $2;
    return "";
});
Luke Schafer
  • 9,209
  • 2
  • 28
  • 29
  • Thanks Luke But when I use ur code as below: function change() { var embeddedString=document.getElementById("Text1").value; embeddedString = embeddedString.replace('/(width\s*=\s*[\"])[0-9]+([\"])/ig', 'width="500"'); document.getElementById("Text2").value=embeddedString; } It doesn't replace any values. I am writing the code in javascript. Am I doing any mistake?. Please correct it. –  May 26 '09 at 04:43
  • Hi mini, do it without the quotes around the regular expression - the //ig syntax is a special syntax for compiled regex' – Luke Schafer May 27 '09 at 03:16
1

Regexes are fundamentally bad at parsing HTML (see Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why). What you need is an HTML parser. See Can you provide an example of parsing HTML with your favorite parser? for examples using a variety of parsers.

You may be interested into the two answers related to JavaScript: jQuery and DOM.

Community
  • 1
  • 1
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
  • +1 For mentioning the use of an appropriate parser. But is it possible to use the jQuery/DOM without having the external ressources being loaded automatically? – Gumbo May 26 '09 at 08:25
  • @Gumbo The DOM is part of the web browser, so it should already be waiting for you. – Chas. Owens May 26 '09 at 13:22
  • 1
    except that they paste the code in so there is no interpretation into a dom by the browser – Luke Schafer May 27 '09 at 03:14
0

This one does it:

width="(.*?)" height="(.*?)"

and grab the first and second values. What language are you using? The implementation might be different.

In PHP:


     $pattern = '/width="(.*?)" height="(.*?)"/';
     $subject = 'width="500" height="300"';

     preg_match($pattern, $subject, $matches);

     print_r($matches);

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • I want to write it in javascript –  May 26 '09 at 04:11
  • Not sure about javascript, but if you can read the PHP code above, you can use the info here to translate it: http://www.regular-expressions.info/javascript.html – nevan king May 26 '09 at 04:16