我需要从输入字符串中排除所有链接,但允许图像链接。
https://www.example.com/X00132BhJk.jpg =>允许
http://www.example.com/search?ident=X00132BhJk =>不允许
所以,假设输入字符串如
arrays
应该只允许P,img标签中的内容,文本标签和A链接中的其他内容应该不通过测试。
如何使用Regex实现这一结果,我根本不擅长Regex!
<p>http://example.com/abc.jpg</p><img src='http://example.com/abc.jpg'/>http://example.com/222.jpg http://example.com/xyz.aspx <a url='http://example.com/abc'/>
更新:
我使用jquery将html解析为html标签,并对每个html节点内容进行测试。
From http://regexr.com/
/(https?:\/\/.*\.(?:png|jpg))/i
=> looks like matches image link, still need a way to exclude normal link without .png, jpg, gif...
var str ="<p>http://example.com/abc.jpg</p><img src='http://example.com/abc.jpg'/> http://example.com/xyz.aspx?id=2 <a url='http://example.com/jpg/3'/><div id='abcd'>http://example.com/xyz.aspx?jpg=2 <br/> http: // example.com/xyz.aspx?jpg=2</div>http://example.com/xyz.aspx?jpg=20 ",
html = $.parseHTML( str ),$log = $( "#log" );
var regex = /^http[^ \!@\$\^&\(\)\+\=]+(\.png|\.jpeg|\.gif|\.jpg)$/;
$.each(html, function(i,el) {
var src='';
switch(el.nodeName)
{
case 'IMG':
src = el.src;break;
case '#text': src= el.textContent;break;
default: src=el.innerHTML;
}
console.log(el.nodeName + ' - ' + src + ' - ' + regex.test(src));
});
答案 0 :(得分:1)
您可以尝试以下正则表达式:
^http.+(png|jpeg|gif|jpg)$
这会使字符串以http
开头,并以指定的格式之一结束
var regex = /^http[^ \!@\$\^&\(\)\+\=]+(\.png|\.jpeg|\.gif|\.jpg)$/;
var test = [
"https://www.example.com/X00132BhJk.jpg",
"http://www.example.com/search?ident=X00132BhJk" ,
"http:// example.com",
"http:// sample.png",
"http://test.com/png"
]
for (var i = 0; i< test.length; i++){
console.log(regex.test(test[i]), test[i])
}
答案 1 :(得分:0)
使用以下正则表达式模式(对于多个网址):
var http = require('http')
exports.handler = (event, context) => {
try {
if (event.session.new) {
console.log("NEW SESSION");
}
switch (event.request.type) {
case "LaunchRequest":
console.log(`LAUNCH REQUEST`);
context.succeed(
generateResponse(
buildSpeechletResponse("Welcome", true),
{}
)
);
break;
case "IntentRequest":
console.log(`INTENT REQUEST`);
switch(event.request.intent.name) {
case "TurnTheLightsOn":
var url = "http://full link";
http.get(url, function(response){
var body = "";
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var data = JSON.parse(body); // {"result":"ok"}
console.log("Got a response: ", data);
context.succeed(
generateResponse(
buildSpeechletResponse(`Ok`)
)
);
});
});
}
break;
case "SessionEndedRequest":
console.log(`SESSION ENDED REQUEST`);
break;
default:
context.fail(`INVALID REQUEST TYPE: ${event.request.type}`);
}
} catch(error) { context.fail(`Exception: ${error}`); }
};
buildSpeechletResponse = (outputText, shouldEndSession) => {
return {
outputSpeech: {
type: "PlainText",
text: outputText
},
shouldEndSession: shouldEndSession
};
};
generateResponse = (speechletResponse, sessionAttributes) => {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
};
};
答案 2 :(得分:0)
两个警告:
允许这两个警告非常接近。您只需要在开头添加^
并在字符串末尾添加$
,以确保jpg
或png
是字符串中的最后一个字符。我还在jpg中添加了一个可选的e
,您可以添加所有其他图片文件扩展名,例如gif
,bmp
等:
^https?:\/\/.+\.(?:png|jpe?g)$