Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解析水球的正則表示式的呈現方式問題 #77

Open
PichuChen opened this issue Jan 16, 2021 · 2 comments
Open

解析水球的正則表示式的呈現方式問題 #77

PichuChen opened this issue Jan 16, 2021 · 2 comments

Comments

@PichuChen
Copy link

遇到的問題: 純粹程式碼寫法問題

parseWaterball (string_utils.js:L155)[https://github.com/robertabcd/PttChrome/blob/88e3562e6536cc0a91dbbb9f65e59036e4b73b79/src/js/string_util.js#L155]

當中的正則表示式

export function parseWaterball(str) {
  var regex = new RegExp(/\x1b\[1;33;46m\u2605(\w+)\x1b\[0;1;37;45m (.+) \x1b\[m\x1b\[K/g);
  var result = regex.exec(str);
  if (result && result.length == 3) {
    return { userId: result[1], message: result[2] };
  } else {
    regex = new RegExp(/\x1b\[24;\d{2}H\x1b\[1;37;45m([^\x1b]+)(?:\x1b\[24;18H)?\x1b\[m/g);
    result = regex.exec(str);
    if (result && result.length == 2) {
      return { message: result[1] };
    }
  }
  return null;
};

其中如果使用了 new RegExp 的話,那應該就不需要外面以 / 包圍了吧?
也就是應該改成

var regex = /\x1b\[24;\d{2}H\x1b\[1;37;45m([^\x1b]+)(?:\x1b\[24;18H)?\x1b\[m/g;

或是

var regex = new RegExp("\x1b\[24;\d{2}H\x1b\[1;37;45m([^\x1b]+)(?:\x1b\[24;18H)?\x1b\[m", "g");

這樣?

@IepIweidieng
Copy link
Contributor

var regex = new RegExp("\x1b\[24;\d{2}H\x1b\[1;37;45m([^\x1b]+)(?:\x1b\[24;18H)?\x1b\[m", "g");

與原式語義有異。

如欲改以 new RegExp() 傳入引號字串的寫法,又要完全符合原式語義時,須要考慮跳脫字元。

因此此式應修正為:

var regex = new RegExp("\\x1b\\[24;\\d{2}H\\x1b\\[1;37;45m([^\\x1b]+)(?:\\x1b\\[24;18H)?\\x1b\\[m", "g");

但此寫法不如使用 /pattern/flag 語法般來得簡潔。

IepIweidieng added a commit to ccns/PttChrome that referenced this issue Dec 15, 2021
This solves robertabcd#77

Regex literals (codes like `/pattern/flags`) are already instances of RegExp,
thus `new RegExp(/pattern/flags)` creates an extra copy of the RegExp object,
which is often not intended.

This issue is fixed by rewritten `new RegExp()` codes into plain regex literals.
IepIweidieng added a commit to ccns/PttChrome that referenced this issue Dec 15, 2021
This solves robertabcd#77

Regex literals (codes like `/pattern/flags`) are already instances of RegExp,
thus `new RegExp(/pattern/flags)` creates an extra copy of the RegExp object,
which is often not intended.

This issue was introduced in iamchucky/PttChrome.

This issue is fixed by rewritten `new RegExp()` codes into plain regex literals.
IepIweidieng added a commit to ccns/PttChrome that referenced this issue Dec 15, 2021
This solves robertabcd#77

Regex literals (codes like `/pattern/flags`) are already instances of RegExp,
thus `new RegExp(/pattern/flags)` creates an extra copy of the RegExp object,
which is often not intended.

This issue was introduced in iamchucky/PttChrome.

This issue is fixed by rewritten `new RegExp()` codes into plain regex literals.
IepIweidieng added a commit to ccns/PttChrome that referenced this issue Dec 15, 2021
This solves robertabcd#77

Regex literals (codes like `/pattern/flags`) are already instances of RegExp,
thus `new RegExp(/pattern/flags)` creates an extra copy of the RegExp object,
which is often not intended.

This issue was introduced in iamchucky/PttChrome.

This issue is fixed by rewritten `new RegExp()` codes into plain regex literals.
@IepIweidieng
Copy link
Contributor

IepIweidieng commented Dec 15, 2021

new RegExp() 是必要的。見:
#103 (comment)

因爲這個正規表達式字面值具有 g (全域搜尋) 旗標,而底下執行了 regex.exec(),因此 regex.lastIndex (字串匹配的起點) 會被更動。
但由於正規表達式字面值所代表的物件只會在 JavaScript 腳本載入時建立一次,如果去掉了 new RegExp(),變數 regex 就會只指向同一個 RegExp 物件,造成 regex.lastIndex 不會重設。

new RegExp() 會確保每次執行該函式時,變數 regex 都會指向新建立的 RegExp 物件,而執行 regex.exec() 時就不會更動到正規表達式字面值所代表的物件,可以避免 regex.lastIndex 不會重設的問題。

另外,由於 new RegExp() 的引數是 RegExp 物件時,會直接使用複製物件的方式建立物件,不須重新從字串編譯 RegExp 物件,這樣寫的效能也會比較好。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants