[英]Remove part of attribute value with jquery or javascript
There is a data parameter for a div that looks as follows: div的数据参数如下所示:
<div data-params="[possibleText&]start=2011-11-01&end=2011-11-30[&possibleText]">
</div>
I want to remove the from the start
through the end of the second date from that data-params attribute. 我想从该data-params属性中删除从第二个日期
start
到第二个日期的结束。 There may or may not be text before the start
and after the date after the second date. 在
start
日期之前和第二个日期之后的日期之后可能没有文本。
How can I accomplish this using javascript or jQuery? 如何使用JavaScript或jQuery完成此操作? I know how to get the value of the "data-params" attribute and how to set it, I'm just not sure how to remove just that part from the string.
我知道如何获取“数据参数”属性的值以及如何对其进行设置,但我不确定如何从字符串中仅删除该部分。
Thank you! 谢谢!
Note: The dates will not always be the same. 注意:日期不会总是相同。
I'd use a regular expression : 我会使用正则表达式 :
var text = $('div').attr('data-params');
var dates = text.match(/start=\d{4}-\d{2}-\d{2}&end=\d{4}-\d{2}-\d{2}/)[0]
// dates => "start=2011-11-01&end=2011-11-30"
The regular expression is not too complex. 正则表达式不太复杂。 The notation
\\d
means "match any digit" and \\d{4}
means "match exactly 4 digits". \\d
表示“匹配任何数字”, \\d{4}
表示“完全匹配4个数字”。 The rest is literal characters. 其余为文字字符。 So you can see how it works.
这样您就可以了解其工作原理。 Finally, that
[0]
at the end is because javascript match returns an array where the first element is the whole match and the rest are subgroups. 最后,末尾的
[0]
是因为javascript match返回一个数组,其中第一个元素是整个匹配项,其余元素是子组。 We don't have any subgroups and we do want the whole match, so we just grab the first element, hence [0]
. 我们没有任何子组,我们确实想要整个比赛,所以我们只抓第一个元素,因此
[0]
。
If you wanted to pull out the actual dates instead of the full query string, you can create subgroups to match by adding parenthesis around the parts you want, like this: 如果要提取实际日期而不是完整的查询字符串,则可以通过在想要的部分周围添加括号来创建匹配的子组,如下所示:
var dates = text.match(/start=(\d{4}-\d{2}-\d{2})&end=(\d{4}-\d{2}-\d{2})/)
// dates[0] => "start=2011-11-01&end=2011-11-30"
// dates[1] => "2011-11-01"
// dates[2] => "2011-11-30"
Here, dates[1]
is the start date (the first subgroup based on parenthesis) and dates[2]
is the end date (the second subgroup). 在此,
dates[1]
是开始日期(基于括号的第一个子组), dates[2]
是结束日期(第二个子组)。
My regex skills aren't that good but this should do it 我的正则表达式技能不是很好,但这应该可以做到
var txt = "[possibleText&]start=2011-11-01&end=2011-11-30[&possibleText]";
var requiredTxt = txt.replace(/^(.*)start=\d{4}-\d{2}-\d{2}&end=\d{4}-\d{2}-\d{2}(.*)$/, "$1$2");
I'm sure there are better ways to match your string with regex, but the $1 and $2 will put the first group and second group match into your requiredTxt stripping out the start/end stuff in the middle. 我确定有更好的方法将您的字符串与正则表达式匹配,但是$ 1和$ 2将把第一组和第二组匹配放入您的requiredTxt中,以去除中间的开始/结束内容。
Say you have your data-params
in a variable foo
. 假设您将
data-params
放在变量foo
。 Call foo.match
as follows: 如下调用
foo.match
:
foo.match("[\\?&]start=([^&#]*)"); //returns ["&start=2011-11-01", "2011-11-01"]
foo.match("[\\?&]end=([^&#]*)"); //returns ["&end=2011-11-30", "2011-11-30"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.