javascript替换换行符和特殊字符 [英] javascript replace line breaks AND special characters

查看:149
本文介绍了javascript替换换行符和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在警报中添加了一个".replace",以删除/替换换行符和硬性回执,效果很好.

I added a ".replace" to my alert to remove/replace line breaks and hard returns and it works great.

alert((foo + bar).replace(/(\n|\r)/g,''));

我想添加一个类似的字符来替换特殊字符.可以单独使用,但是如何将两者结合使用,以消除中断和特殊字符?

I want to add a similar piece to replace special characters. This works on it's own, but how do I combine the two so it removes breaks AND spec characters?

 .replace(/[^a-zA-Z0-9]/g,'_');

这是我最好的猜测,它不起作用....

This was my best guess at it and it's not working....

 alert((foo + bar).replace(/(\n|\r)/g,''),(/[^a-zA-Z0-9]/g,'_'));

推荐答案

如果您要替换为示例中显示的其他字符串

If you want to replace with different strings like you showed in your example

alert((foo + bar).replace(/(\n|\r)/g,'').replace(/[^a-zA-Z0-9]/g,'_'));

如果两个都可以替换为空字符串

If both can be replaced with an empty string

alert((foo + bar).replace(/(\n|\r|[^a-zA-Z0-9])/g,'')

这篇关于javascript替换换行符和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆