从字符串中删除特殊字符以通过jQuery过滤数据

时间:2017-04-04 03:11:30

标签: javascript jquery regex

我有一个这样的字符串:

var str = "f2025%20%3D%20%2209%3A00%22%20and%20f5118288%20%3D%20%222017-03-31%22#";

如何删除所有特殊字符{'%20%3D%20%22','%3A','%22%20',& #39;%20'}从上面的字符串返回列表值,如{' f2025',' 09',' 00',' ;和',' f5118288',' 2017-03-31'}

2 个答案:

答案 0 :(得分:2)

您可以使用以下正则表达式来拆分字符串

%20%3D%20%22|%3A|%22%20|%20|%22#

使用此split function

var str = "f2025%20%3D%20%2209%3A00%22%20and%20f5118288%20%3D%20%222017-03-31%22#";
var strArray = str.split(/%20%3D%20%22|%3A|%22%20|%20|%22#/)

答案 1 :(得分:0)

您可以使用带有特殊字符的replacesplit来获得结果。

var str = "f2025%20%3D%20%2209%3A00%22%20and%20f5118288%20%3D%20%222017-03-31%22#";
str = str.replace(/%20%3D%20%22/g, "@").replace(/%3D/g, "").replace(/%3A/g, "@").replace(/%22%20/g, "@").replace(/#/g, "").replace(/%20/g, "@").replace(/%22/g, "@");
var Result = str.split('@');
alert(Result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>