繁体   English   中英

删除特定符号后的空格

[英]Remove whitespace after a particular symbol

我有一个字符串,它多次带有>符号。 每个>符号后都有一个换行符。 如何删除字符串中每个>符号后的空格?

这就是我尝试的空间。 但我只需要在>符号之后删除空格。

str.replace(/\s/g, '');

细绳:

<Apple is red>
<Grapes - Purple>
<Strawberries are Red>

尝试这个:

str.replace(/>\s/g, '>')

演示:

 console.log(`<Apple is red> <Grapes - Purple> <Strawberries are Red>`.replace(/>\\s/g, '>'))

如果您尝试删除换行符,您可以使用RegExp /(>)(\\n)/g用替换空字符串""替换第二个捕获组(\\n)

 var str = `<Apple is red> <Grapes - Purple> <Strawberries are Red>`; console.log(`str:${str}`); var res = str.replace(/(>)(\\n)/g, "$1"); console.log(`res:${res}`);

使用以下方法:

 var str = 'some> text > the end>', replaced = str.replace(/(\\>)\\s+/g, "$1"); console.log(replaced);

  1. 您必须在正则表达式模式上使用 /m 标志

  2. 您必须在替换文本上使用 $1 捕获组符号

    var text = '<Apple is red>\\r\\n<Grapes - Purple>\\r\\n<Strawberries are Red>'; var regex = /(>)\\s*[\\n]/gm; var strippedText = text.replace(regex,'$1');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM