0
私はこれをanswerと読みましたが、Regexコンストラクタの使い方を理解しています。 test()メソッドはtrueを返しません。助けてください!変数を使って正規表現のメソッドを呼び出せません。
// This returns true
console.log(/HYD\./gi.test("HYD.,CYLINDER"))
// Now I will use a variable inside this test method so
// I have to use the Regex constructor
const abbreviation = "HYD.";
// I have to replace all "." with "\." to be compliant with the regex language.
const regex = new RegExp(`\\b${abbreviation.replace(".", "\\.")}\\b`, "gi");
console.log(regex)
// Now testing the regex constructor
console.log(regex.test("HYD.,CYLINDER"));
問題は、ドットとカンマの間に一致しない2番目の '\ b'です。 – georg