所以我有这个问题,我有一个字符串。当发送给服务时,我给出了一个对象数组,我需要用它来替换该字符串中指定位置的单词。但是我想跳过已经包含在锚标记中的任何返回的内容。
例如此字符串:
var str = "<p>Gilbert Arenas is still a <inline id='something0-container' class='something-container linkContainer' contenteditable='false' data-content='ANNOTATION '><inline class='something' id='something0'><div class='something-preview'>" +
" <a href='#'>Washington Wizards</a> " +
"</div></inline></inline> fan. The above Instagram photo -- especially the caption -- is evidence of this. The former All-Star guard didn't wait to find out the Wizards' first-round opponent to start talking trash -- both the Toronto Raptors and Chicago Bulls were targets.</p><p>Honestly, though, I'm a little disappointed in this. Dissing the Canadian anthem? Frying bologna? Arenas can do better, I'm sure. Really hope Drake -- or Masai Ujiri, perhaps? -- fires back.</p>";
这个对象
[{
"canon": "Dakota Wizards",
"text": "Wizards",
"begin": "258",
"end": "265",
}, {
"canon": "Dakota Wizards",
"text": "Wizards",
"begin": "430",
"end": "437",
}, {
"canon": "Toronto Raptors",
"text": "Toronto Raptors",
"begin": "495",
"end": "510",
}, {
"canon": "Chicago Bulls",
"text": "Chicago Bulls",
"begin": "515",
"end": "528",
}]
我需要确定&#34; Wizards&#34;是否会出现?位置258被包裹在一个锚标签中(在这个例子中它应该是)。
编辑:第一次输入字符串时意外删除了空格。修复了这里
答案 0 :(得分:1)
尝试
var str = "<p>Gilbert Arenas is still a <inline id='something0-container' class='something-container' linkContainer contenteditable='false' data-content='ANNOTATION '><inline class='something' id='something0'><div class='something-preview'><a href='#'>Washington Wizards</a></div></inline</inline> fan. The above Instagram photo -- especially the caption -- is evidence of this. The former All-Star guard didn't wait to find out the Wizards' first-round opponent to start talking trash -- both the Toronto Raptors and Chicago Bulls were targets.</p><p>Honestly, though, I'm a little disappointed in this. Dissing the Canadian anthem? Frying bologna? Arenas can do better, I'm sure. Really hope Drake -- or Masai Ujiri, perhaps? -- fires back.</p>";
var data = [{
"canon": "Dakota Wizards",
"text": "Wizards",
"begin": "258",
"end": "265",
}, {
"canon": "Dakota Wizards",
"text": "Wizards",
"begin": "430",
"end": "437",
}, {
"canon": "Toronto Raptors",
"text": "Toronto Raptors",
"begin": "495",
"end": "510",
}, {
"canon": "Chicago Bulls",
"text": "Chicago Bulls",
"begin": "515",
"end": "528",
}];
var res = $.parseHTML(str.slice(data[0].begin, data[0].end).replace(/\//, ""))
.some(function(el) {
return el.nodeName === "A"
});
console.log(res); // `true`
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
答案 1 :(得分:1)
也许这会有所帮助:
var str = "<p>Gilbert Arenas is still a <inline id='something0-container' class='something-container linkContainer' contenteditable='false' data-content='ANNOTATION '><inline class='something' id='something0'><div class='something-preview'> <a href='#'>Washington Wizards</a> </div></inline></inline> fan. The above Instagram photo -- especially the caption -- is evidence of this. The former All-Star guard didn't wait to find out the Wizards' first-round opponent to start talking trash -- both the Toronto Raptors and Chicago Bulls were targets.</p><p>Honestly, though, I'm a little disappointed in this. Dissing the Canadian anthem? Frying bologna? Arenas can do better, I'm sure. Really hope Drake -- or Masai Ujiri, perhaps? -- fires back.</p>"
var arr = [{
"canon": "Dakota Wizards",
"text": "Wizards",
"begin": "258",
"end": "265",
}, {
"canon": "Dakota Wizards",
"text": "Wizards",
"begin": "430",
"end": "437",
}, {
"canon": "Toronto Raptors",
"text": "Toronto Raptors",
"begin": "495",
"end": "510",
}, {
"canon": "Chicago Bulls",
"text": "Chicago Bulls",
"begin": "515",
"end": "528",
}]
//note that this works if your text does not contain < tags, if it does, you need to modify this a bit
for(var i=0;i<arr.length;i++){
subStr = str.substr(arr[i].begin, arr[i].end-arr[i].begin)
if(subStr == arr[i].text){ //matches, lets check if its in an anchor
indx = str.substr(0, arr[i].begin).lastIndexOf("<") //get last < tag before our text
if(indx != -1){
if(str.substr(indx, 2) == '<a'){ //yay its in a <a> tag
console.log(arr[i].text + " is inside <a> ")
}
}
}
}
&#13;
答案 2 :(得分:0)
if($('a:contains("Wizards")').length){
//there is at least one instance of Wizards in an <a> tag
}
答案 3 :(得分:0)
修改了@ juvian的答案。工作小提琴http://jsfiddle.net/DrewLandgrave/05wz1fLw/
/**
* Checks to see if text is in between a specified tag in a string
*
* @param {string} string The entire string to search
* @param {string} searchString the string you want to determine whether or not it's inside of a tag
* @param {string} tagName The tag name you want to see if your string is in between
* @param {string|int} start The beginning string position of the searchString in your string
* @param {string|int} end The end string position of the searchString in your string
*
* @returns {boolean}
*/
insideTag = function(string, searchString, tagName, start, end){
start = parseInt(start) || string.indexOf(searchString);
end = parseInt(end) || start + searchString.length;
var subStr = string.substr(start, end - start),
startTag = '<' + tagName,
endTag = '</' + tagName,
indx;
if (subStr == searchString) { //matches, lets check if its in a tag
indx = string.substr(0, start).lastIndexOf("<") //get last < tag before our text
if (indx != -1) {
if (string.substr(indx, tagName.length + 1) == startTag) { //yay its in a tag
return start > parseInt(indx) && (end < string.indexOf(endTag, indx) + endTag.length)
}
}
}
return false;
}