你好
我有一个应用程序,用户可以在其中粘贴他们的嵌入代码。在显示时,我想将嵌入代码的高度和宽度更改为我选择的固定值。你能告诉我在嵌入代码中找到宽度和高度的正则表达式吗?宽度="300"高度="500"。我希望能够完整地获取这两个值,以便我可以替换它们。
谢谢
最佳答案
以下示例考虑了顺序、使用的引号以及是否有人放入空格
直接替换:
embeddedString = embeddedString.replace(/(width\s*=\s*["'])[0-9]+(["'])/ig, $1 + yourWidth + $2);
embeddedString = embeddedString.replace(/(height\s*=\s*["'])[0-9]+(["'])/ig, $1 + yourHeight + $2);
或者变换宽度:
embeddedString = embeddedString.replace(/width\s*=\s*["']["']/ig, function($0, $1)
{
return $1 * 2;
});
如果你真的想删除整个东西,但使用值:
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 "";
});
关于javascript - 用于在嵌入代码中更改高度和宽度值的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/908863/