【发布时间】:2017-06-13 00:17:28
【问题描述】:
是否可以在 C++ 中创建 RegExp javascript 对象?我只是好奇。
例子:
const myCLib = require('myCLib');
myCLib("/\\s/", "g"); => return regex object /\g/g
【问题讨论】:
是否可以在 C++ 中创建 RegExp javascript 对象?我只是好奇。
例子:
const myCLib = require('myCLib');
myCLib("/\\s/", "g"); => return regex object /\g/g
【问题讨论】:
是的,这是自 C++11 以来 C++ 标准的一部分,不需要额外的库。正则表达式的默认语法与 ECMAScript (JavaScript) 的正则表达式语法相同。
#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)"};
/g 修饰符行为是通过不同地使用正则表达式来实现的,请参阅std::regex equivalent of '/g' global modifier
【讨论】: