-2

I have this Regex :-

let rgb = new RegExp(/rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);

Now, this is working just fine on "rgb( 255 , 0, 255 )"

But I need to write and test this in VS Code, Node.js gives syntax errors when I pass the regex like this, so I'm passing it as a string :

let rgb = new RegExp("rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)");

But this regex isn't validating my string. Need help to use this regex in Node.js

alotropico
  • 1,904
  • 3
  • 16
  • 24
Maaz Ahmad Khan
  • 142
  • 1
  • 11
  • 1
    What is it not validating? May you show some examples of that? – evolutionxbox Jul 19 '20 at 10:25
  • ex: rgb( 255, 15 , 255). My issue is that browser is accepting this regex as parameter in new RegExp(/rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/) but node.js gives syntax errors. why is this so? – Maaz Ahmad Khan Jul 19 '20 at 10:35

1 Answers1

0

Try adding the regex like this

let rgb = /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/

Then test it like this

rgb.exec(string_to_test_against);

Read more about js regex here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Kirmin
  • 301
  • 2
  • 7
  • this method works. no syntax error when I do it like this in node.js :- let regex = /rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/; let newRegEx = new RegExp(regex); console.log("test " + newRegEx.test("rgb( 255 , 15 , 120 )")); – Maaz Ahmad Khan Jul 19 '20 at 10:31