簡體   English   中英

刪除多行文本區域文本中每行“-”之前的所有字符

[英]Remove all characters before “-” on each line of multiline textarea text

我從多行文本區域中獲取了一些文本,每行由換行符分隔:

 1 - test1
 2 - test2
 3 - test3
 4 - test4

如何刪除-之前的所有文本,所以文本看起來像:

 test1
 test2
 test3
 test4

您可以查找沒有破折號和破折號,並用空字符串替換多行數據。

 var string = ' 1 - test1\\n 2 - test2\\n 3 - test3\\n 4 - test4', result = string.replace(/^[^-]+-/gm, ''); console.log(result); 

您可以使用split和splice進行操作。

 const mytext = ' 1 - test1'; const arr = mytext.split('-'); const splice = arr.splice(1,1 ,''); const final = splice.toString(); console.log(final); 

這可以用正則表達式解決。 嘗試放下字符串以檢查是否可行。

這是一個不錯的網站: https : //regex101.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM