Is it possible to create a RegExp javascript object, in C++? I'm just curious about it.
Example:
const myCLib = require('myCLib');
myCLib("/\\s/", "g"); => return regex object /\g/g
Is it possible to create a RegExp javascript object, in C++? I'm just curious about it.
Example:
const myCLib = require('myCLib');
myCLib("/\\s/", "g"); => return regex object /\g/g
Yes, this has been part of the C++ standard since C++11 and no additional libraries are needed. The default syntax for regular expressions is identical to ECMAScript's (JavaScript's) syntax for regular expressions.
#include <regex>
// Note that construction of the regex can be QUITE expensive, and should
// not be done often if you care even a little about performance. Regexes
// are commonly created at global scope.
const std::regex my_regex{"\\s"};
// Raw strings are sometimes more readable...
const std::regex my_regex{R"(\s)"};
The /g modifier behavior is achieved by using the regex differently, see std::regex equivalent of '/g' global modifier