Javascript 不利因素及;备选方案-JS方法链接
我正在写一些代码,按照这些思路做一些事情Javascript 不利因素及;备选方案-JS方法链接,javascript,replace,Javascript,Replace,我正在写一些代码,按照这些思路做一些事情 txt = txt.replaceAll('\n','').replaceAll('<b>','[bold]').replaceAll('</b>','[/bold]') .replaceAll('<strong>','[bold]').replaceAll('</strong>','[/bold]').... txt=txt.replaceAll('\n',').replaceAll('''[bold]
txt = txt.replaceAll('\n','').replaceAll('<b>','[bold]').replaceAll('</b>','[/bold]')
.replaceAll('<strong>','[bold]').replaceAll('</strong>','[/bold]')....
txt=txt.replaceAll('\n',').replaceAll('''[bold]').replaceAll('''[/bold]'))
.replaceAll(“”、“[bold]”)。replaceAll(“”、“[/bold]”)。。。。
其中replaceAll是String.prototype扩展名。这很好用,但我想知道-
以这种方式链接太多的方法有什么缺点吗?使用在“oner”中完成这项工作的正则表达式是否更好?如果是这样,正则表达式会是什么样子?(我对正则表达式不是很在行)这很好。正则表达式的替代方法也很简单,你基本上只需要使用一个替代方法,并确保逃避需要逃避的东西:|
var替换={
“\n”:”,
'':''[粗体]'
'':''[/bold]',
“”:“[bold]”,
“”:“[/bold]”
// ...
};
txt=txt.replace(/\n | | ||/g,函数(m){
在替换中返回m?替换[m]:m;
});
可以将这些链接在一起。但不要把它们放在一条线上。如果重新格式化代码,阅读起来会容易得多:
txt = txt
.replaceAll( '\n', '' )
.replaceAll( '<b>', '[bold]' )
.replaceAll( '</b>', '[/bold]' )
.replaceAll( '<strong>', '[bold]' )
.replaceAll( '</strong>', '[/bold]' );
txt=txt
.replaceAll('\n','')
.replaceAll(“,[粗体]”)
.replaceAll(“”,[/bold]'))
.replaceAll(“”,“[bold]”)
.replaceAll(“”、“[/bold]”);
这种样式在jQuery链中也很有用:
$('<div>Test</div>')
.css({ fontSize: '16px' })
.attr({ title: 'Test' })
.appendTo( 'body' );
$(“测试”)
.css({fontSize:'16px'})
.attr({title:'Test'})
.appendTo(‘body’);
@迈克尔·盖里。我接受T.J.Crowder的答案是因为正则表达式。
$('<div>Test</div>')
.css({ fontSize: '16px' })
.attr({ title: 'Test' })
.appendTo( 'body' );