从HTML中删除字符串?

时间:2022-09-06 16:28:15

In my code, I send get requests to a site and sometimes the return has strings such as the following at the top above the HTML, causing jQuery to error

在我的代码中,我将get请求发送到一个站点,有时返回的字符串如HTML上方的顶部,导致jQuery错误

For example

4341-b087-58848cd597a7%26mode%3d%26code%3d500|<div>Div</div>

Which would result in

哪会导致

 Uncaught Error: Syntax error, unrecognized expression: 4341-b087-58848cd597a7%26mode%3d%26code%3d500|jquery.js:1473 Sizzle.errorjquery.js:2087 Sizzle.tokenizejquery.js:2488 Sizzle.selectjquery.js:879 Sizzlejquery.js:2704 jQuery.fn.extend.findjquery.js:2821 jQuery.fn.initjquery.js:73 jQuerygear.js:26 (anonymous function)gear.js:21 (anonymous function)jquery.js:3119 firejquery.js:3231 self.fireWithjquery.js:9275 donejquery.js:9685 callback

Each time the strings have something in common; they all begin with 4, end with %26mode%3d%26code%3d500|

每次弦都有共同点;它们都以4开头,以%26mode%3d%26code%3d500 |结束

How can I check if the text %26mode%3d%26code%3d500| is in the page, and if so remove everything from 4 to %26mode%3d%26code%3d500| (including those) so it does not error? My code is something like this

如何检查文本%26mode%3d%26code%3d500 |在页面中,如果是这样,删除4到%26mode%3d%26code%3d500 |的所有内容(包括那些)所以它没有错误?我的代码是这样的

$.get(url).done(function(data) {
    callback(data.replace(/img/gi, "flip");
}

Thanks!

3 个解决方案

#1


2  

You can use a regexp:

你可以使用正则表达式:

data = data.replace(/^4.*%26mode%3d%26code%3d500\|/, '');

Or you can wrap the whole thing in a DIV:

或者您可以将整个事物包装在DIV中:

var Item = $('<div>', { html: data }).find("#ct100").attr("href");

#2


0  

If the string is at the beginning of the response, simple & fast:

如果字符串位于响应的开头,则简单快速:

$.get(url).done(function(data) {
    data = data.split('%26mode%3d%26code%3d500|')[1];
}

#3


0  

you can even use javascript string functions to get rid of this:

你甚至可以使用javascript字符串函数来摆脱这个:

var pos = data.lastIndexOf("%26mode%3d%26code%3d500|");
var reqPos = pos + 24; //last position of above string + number of chars in above string
var result = str.slice(reqPos);
alert(result); //will give you expected output

#1


2  

You can use a regexp:

你可以使用正则表达式:

data = data.replace(/^4.*%26mode%3d%26code%3d500\|/, '');

Or you can wrap the whole thing in a DIV:

或者您可以将整个事物包装在DIV中:

var Item = $('<div>', { html: data }).find("#ct100").attr("href");

#2


0  

If the string is at the beginning of the response, simple & fast:

如果字符串位于响应的开头,则简单快速:

$.get(url).done(function(data) {
    data = data.split('%26mode%3d%26code%3d500|')[1];
}

#3


0  

you can even use javascript string functions to get rid of this:

你甚至可以使用javascript字符串函数来摆脱这个:

var pos = data.lastIndexOf("%26mode%3d%26code%3d500|");
var reqPos = pos + 24; //last position of above string + number of chars in above string
var result = str.slice(reqPos);
alert(result); //will give you expected output