[英]Remove whitespace after a particular symbol
I've got a string which has the >
symbol multiple times.我有一个字符串,它多次带有
>
符号。 After every >
symbol there is a line break.每个
>
符号后都有一个换行符。 How can I remove whitespaces after every >
symbol in the string?如何删除字符串中每个
>
符号后的空格?
This is what I tried for spaces.这就是我尝试的空间。 But I need to remove whitespaces only after
>
symbol.但我只需要在
>
符号之后删除空格。
str.replace(/\s/g, '');
String:细绳:
<Apple is red>
<Grapes - Purple>
<Strawberries are Red>
Try this:尝试这个:
str.replace(/>\s/g, '>')
Demo:演示:
console.log(`<Apple is red> <Grapes - Purple> <Strawberries are Red>`.replace(/>\\s/g, '>'))
If you are trying to remove newline characters you can use RegExp
/(>)(\\n)/g
to replace second capture group (\\n)
with replacement empty string ""
如果您尝试删除换行符,您可以使用
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}`);
Use the following approach:使用以下方法:
var str = 'some> text > the end>', replaced = str.replace(/(\\>)\\s+/g, "$1"); console.log(replaced);
You must use /m flag on your regex pattern您必须在正则表达式模式上使用 /m 标志
You must use $1 capture group symbol on your replacement text您必须在替换文本上使用 $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.