簡體   English   中英

Javascript 使用 RegEXP 去除(但不包括)特殊字符之間的字符

[英]Javascript use RegEXP to remove characters between (but not including) special characters

我有一個字符串如下:

var s = "1111 type reallycoolsentence\text.json\n1111 type anotherreallycoolsentence text2.json

我試圖擺脫反斜杠之間的字符。

想要的結果:

s = "type reallycoolsentence\\type anotherreallycoolsentence"

我知道如何在不刪除特殊字符的情況下刪除兩個特殊字符之間的字符以外的所有內容。 堆棧上的每個答案也包括刪除它們:(

將反斜杠放在替換字符串中。

請注意,您需要將它們加倍以獲得文字反斜杠,因為反斜杠是字符串文字中的轉義前綴。

 var s = "1111 type reallycoolsentence\\text.json\\n1111 type anotherreallycoolsentence text2.json"; var result = s.replace(/\\.*\\/, '\\\\'); console.log(result);

此結果與您的示例中的結果不匹配,但那是因為它與您對要執行的操作的描述不匹配。 我實現了描述。

對於我們這些不喜歡正則表達式的人......:

  s = "1111 type reallycoolsentence text.json\n1111 type anotherreallycoolsentence text2.json"
  wArray = s.split(" ");  
  wArray = wArray.filter( value => value !== "1111");
  wArray = wArray.filter(value => !value.includes('.json'));
  result = wArray.join(" ");

Output:

type reallycoolsentence type anotherreallycoolsentence

暫無
暫無

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

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