gpt4 book ai didi

javascript - 搜索字符串并捕获特定 HTML 属性的值

转载 作者:行者123 更新时间:2023-11-30 11:39:24 25 4
gpt4 key购买 nike

我需要一个正则表达式来获取所有后续字符串的 src 值,考虑单引号、双引号和无引号。

双引号:

var a = '<script class="anyClass" src="anyFile.js" id="anyID">';
var b = '<script class="anyClass" src="anyFile.js">';
var c = '<script src="anyFile.js" id="anyID">';
var d = '<script src="anyFile.js">';

带单引号:

var e = "<script class='anyClass' src='anyFile.js' id='anyID'>";
var f = "<script class='anyClass' src='anyFile.js'>";
var g = "<script src='anyFile.js' id='anyID'>";
var h = "<script src='anyFile.js'>";

没有引号:

var i= "<script class=anyClass src=anyFile.js id=anyID>";
var j= "<script class=anyClass src=anyFile.js>";
var k= "<script src=anyFile.js id=anyID>";
var l= "<script src=anyFile.js>";

预期匹配/返回:

anyFile.js

我有一个看起来很糟糕的拆分解决方案:

var file = "";
var match = 'src="';
if(a.indexOf(match)>=0){
file = a.split(match);
file = file[1];
file = file.split('.js"');
file = file[0] + ".js";
}
else{
match = "src='";
if(a.indexOf(match)>=0){
file = a.split(match);
file = file[1];
file = file.split(".js'");
file = file[0] + ".js";
}
else{
match = "src=";
if(a.indexOf(match)>=0){
file = a.split(match);
file = file[1];
file = file.split('.js');
file = file[0] + ".js";
}
else {
file = "no match";
}
}
}

但是它可以匹配错误的 src 属性,所以我需要一个正则表达式。

如有任何帮助,我们将不胜感激。

解决方案(特别感谢 Rajesh)

var a = "<script class='anyClass' src='anyFile.js' id='anyID'>";
var b = '<script class="anyClass" src="anyFile.js">';
var c = '<script src="anyFile.js" id="anyID">';
var d = '<script src="anyFile.js">';

var e = "<script class='anyClass' src='anyFile.js' id='anyID'>";
var f = "<script class='anyClass' src='anyFile.js'>";
var g = "<script src='anyFile.js' id='anyID'>";
var h = "<script src='anyFile.js'>";

var i= "<script class=anyClass src=anyFile.js id=anyID>";
var j= "<script class=anyClass src=anyFile.js>";
var k= "<script src=anyFile.js id=anyID>";
var l= "<script src=anyFile.js>";
var l= "<script src=anyFile.js>";

function getSrc(str){
var regex = /src=["']*[^"' >]+/;
var match = str.match(regex);
if(match!==null){
match = match[0];
var replaceRegex = /src=["' ]*/;
match = match.replace(replaceRegex, "")
}
else{
match = "no match";
}
console.log(match);
}

[a,b,c,d,e,f,g,h,i,j,k,l].forEach(getSrc)

最佳答案

你可以尝试这样的事情:

逻辑:

  • 搜索模式 src= 后跟单引号/双引号 [可选] 后跟任何非单引号/双引号的内容。
  • 这将产生类似 src="anyfile.js 的东西。
  • 现在替换首字母,即 src= 后跟可选引号。

正则表达式版本

var a = '<script class="anyClass" src="anyFile.js" id="anyID">';
var b = '<script class="anyClass" src="anyFile.js">';
var c = '<script src="anyFile.js" id="anyID">';
var d = '<script src="anyFile.js">';
var i= "<script class=anyClass src=anyFile.js id=anyID>";
var j= "<script class=anyClass src=anyFile.js>";
var k= "<script src=anyFile.js id=anyID>";
var l= "<script src=anyFile.js>";

function getSrc(str){
var regex = /src=["']*[^"' >]+/;
var match = str.match(regex)[0];
var replaceRegex = /src=["' ]*/;
match = match.replace(replaceRegex, "")
console.log(match)
}

[a,b,c,d,i,j,k,l].forEach(getSrc)

关于javascript - 搜索字符串并捕获特定 HTML 属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43279697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com