簡體   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