我在div中显示了几个项目列表。我可以用这种格式的字符串数据。例如:12H-912
这里12(前2位数)代表最后两个字符。(例如:2012年将是12) 并且“H”代表月份。(对于jan - “A”,feb - “B”... Aug - “H”)。 “ - ”代表当月的一天。 12(最后两个字符代表我生成的主键。这可以是任何字符,不仅仅是两个,我的意思是它可以是2或1223或232323 ..)。
现在在jquery中我想找到这种格式的字符串,我想把它作为超链接,并在其中调用一些函数。任何人都可以帮助找到解决方案。
答案 0 :(得分:0)
您可以使用以下正则表达式来查找完全匹配
"12H-912".match(/^\d{2}[A-Z]\-\w+$/)
答案 1 :(得分:0)
工作数据:
<div>this is a div with 12H-912</div>
<table>
<tr><td>7H-9123123</td></tr>
<tr><td>08A-912234</td></tr>
<tr><td>08132A-912234</td></tr>
</table>
使用Javascript:
$('body').ready(function(){
$('div').each(function(){
var html = $(this).html();
if (html.match(/[0-9]{2}[A-H]{1}-[0-9]+/g)) {
$(this).html(html.replace(/([0-9]{2}[A-H]{1}-[0-9]+)/g, '"$1"'));
}
});
// easier and safer when in a td (alone)
$('td').each(function(){
var html = $(this).html();
if (html.match(/^[0-9]{2}[A-H]{1}-[0-9]+$/g)) {
$(this).html(html.replace(/([0-9]{2}[A-H]{1}-[0-9]+)/g, '"$1"'));
}
});
});
输出:
this is a div with "12H-912"
7H-9123123
"08A-912234"
08132A-912234
我会让你解决这个问题。 你可以在这里弄清楚:http://jsfiddle.net/fCJJb/