Javascript 替换以特定文本开头的元素的文本

Javascript 替换以特定文本开头的元素的文本,javascript,jquery,text,Javascript,Jquery,Text,我有类似html的底层代码 <table> <tr> <td> {name length='100'} </td> <td> {otherText length='100'} </td> <td> {otherText2 length='100'}

我有类似html的底层代码

<table>
    <tr>
        <td>
            {name length='100'}
        </td>
        <td>
            {otherText length='100'}
        </td>
        <td>
            {otherText2 length='100'}
        </td>
    </tr>
</table>

{name length='100'}
{otherText length='100'}
{otherText2 length='100'}
我想更改以
{name
开头的
td
s文本


如何执行此操作?

如果要替换以
{name
开头的整个内部文本,请尝试以下操作:

$('td').html('{name '+new_text);
//添加此条件以在所有浏览器中使用startsWith函数
if(!String.prototype.startsWith){
String.prototype.startsWith=函数(searchString,position){
位置=位置| | 0;
返回此.indexOf(searchString,position)==position;
};
}
函数替换(newVal){
var tds=document.getElementsByTagName(“td”);
对于(变量i=0;i

{name length='100'}

您可以使用jquery
text(function)
迭代所有
td
s文本,并使用
match()
通过正则表达式检查文本内容

$(“td”).text(函数(索引,文本){
if(text.trim().match(/^{name/))
返回“已更改的文本”;
});
表格,tr,td{
边框:1px纯黑;
}

{name length='100'}
{type length='100'}
{其他长度='100'}

您想只替换该行中的
100
还是整行?我知道我可以选择带有(“td:contains('name')”)的行,但这不够具体。我需要某种通配符来匹配文本{name加大括号。@MartinDK81您检查过我的答案了吗?
startWith()
浏览器上版本中的支持。例如,chrome的41版。请参阅。