开发者

Javascript regex question: strip special symbols and URL's

What I need is something that will match words that contain special symbols (except ?, ! and .) and URL's. Here's what I already have, but it 开发者_如何学Pythondoesn't quite work.

re = new RegExp('(http://\S+|\S*[^\w\s,.":]\S*)');

text = '@hello how are you? http://example.com';
clean = text.replace(re, '');

The current output is:

> 'hello how are you? http://example.com'

The output should be:

> '  how are you? '


You need to use global in the RE

text.replace(/(http:\/\/\S+|\S*[^\w\s,.":?]\S*)/g,'');

I also added "?" to your set since it seems you want that included.


re = /(http:\/\/\S+|\S*[^\w\s,\.\?":]\S*)/ig;

text = '@hello how are you? http://example.com';
clean = text.replace(re, '');


I don't know much about JavaScript, in Java I'll do this:

clean = input.replaceAll("(http://\\S+|\\S*[^\\w\\s\\?,.\"!:]\\S*)", "");

If you don't need to escape special characters, remove the backslashes.

Regards!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜