You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/guide/regex-engines.md
+21-7
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ outline: deep
4
4
5
5
# RegExp Engines
6
6
7
-
TextMate grammars is based on regular expressions to match tokens. Usually, we use [Oniguruma](https://github.com/kkos/oniguruma) (a regular expression engine written in C) to parse the grammar. To make it work in JavaScript, we compile Oniguruma to WebAssembly to run in the browser or Node.js.
7
+
TextMate grammars are based on regular expressions to match tokens. Usually, we use [Oniguruma](https://github.com/kkos/oniguruma) (a regular expression engine written in C) to parse the grammar. To make it work in JavaScript, we compile Oniguruma to WebAssembly to run in the browser or Node.js.
8
8
9
9
Since v1.15, we expose the ability to for users to switch the RegExp engine and provide custom implementations.
This feature is experimental and may change without following semver.
44
44
:::
45
45
46
-
This experimental engine uses JavaScript's native RegExp. As TextMate grammars' regular expressions are in Oniguruma flavor that might contains syntaxes that are not supported by JavaScript's RegExp, we use [`oniguruma-to-js`](https://github.com/antfu/oniguruma-to-js) to lowering the syntaxes and try to make them compatible with JavaScript's RegExp.
46
+
This engine uses JavaScript's native RegExp. As regular expressions used by TextMate grammars are written for Oniguruma, they might contain syntax that is not supported by JavaScript's RegExp, or expect different behavior for the same syntax. So we use [Oniguruma-To-ES](https://github.com/slevithan/oniguruma-to-es) to transpile Oniguruma patterns to native JavaScript RegExp.
const html =shiki.codeToHtml('const a = 1', { lang: 'javascript', theme: 'nord' })
61
61
```
62
62
63
-
Please check the [compatibility table](/references/engine-js-compat)to check the support status of the languages you are using.
63
+
Please check the [compatibility table](/references/engine-js-compat)for the support status of the languages you are using.
64
64
65
-
If mismatches are acceptable and you want it to get results whatever it can, you can enable the `forgiving` option to suppress any errors happened during the conversion:
65
+
Unlike the Oniguruma engine, the JavaScript engine is strict by default. It will throw an error if it encounters a pattern that it cannot convert. If mismatches are acceptable and you want best-effort results whenever possible, you can enable the `forgiving` option to suppress any errors that happened during the conversion:
If you runs Shiki on Node.js (or at build time), we still recommend using the Oniguruma engine for the best result, as most of the time bundle size or WebAssembly support is not a concern.
73
+
If you run Shiki on Node.js (or at build time) and bundle size or WebAssembly support is not a concern, we still recommend using the Oniguruma engine for the best result.
74
74
75
-
The JavaScript engine is more suitable for running in the browser in some cases that you want to control the bundle size.
75
+
The JavaScript engine is best when running in the browser and in cases when you want to control the bundle size.
76
76
:::
77
+
78
+
### JavaScript Runtime Target
79
+
80
+
For the most accurate result, [Oniguruma-To-ES](https://github.com/slevithan/oniguruma-to-es) requires the [RegExp `v` flag support](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets), which is available in Node.js v20+ and ES2024 ([Browser compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets#browser_compatibility)).
81
+
82
+
For older environments, it can simulate the behavior but `u` flag but might yield less accurate results.
83
+
84
+
By default, it automatically detects the runtime target and uses the appropriate behavior. You can override this behavior by setting the `target` option:
85
+
86
+
```ts
87
+
const jsEngine =createJavaScriptRegexEngine({
88
+
target: 'ES2018', // or 'ES2024', default is 'auto'
0 commit comments