Created
May 31, 2023 12:45
-
-
Save kjohnson/e049ad666ecb63df17a4477c4b45d298 to your computer and use it in GitHub Desktop.
REGEX for WordPress i18n functions in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Generated using ChatGPT | |
const jsCode = ` | |
function exampleFunction() { | |
// Some code here | |
} | |
const translatedText = __("Hello, world!"); | |
const translatedText2 = _x("Hello", "Greeting"); | |
const translatedText3 = _n("One item", "%d items", 5); | |
const translatedText4 = _nx("One item", "%d items", 5, "Text domain"); | |
const notAFunction = "Just a string"; | |
`; | |
const regex = /(?:__|_x|_n|_nx)\s*\(.*?\)/g; | |
const matches = jsCode.match(regex); | |
console.log(matches); | |
// [ | |
// '__("Hello, world!")', | |
// '_x("Hello", "Greeting")', | |
// '_n("One item", "%d items", 5)', | |
// '_nx("One item", "%d items", 5, "Text domain")' | |
// ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(?:__|_x|_n|_nx)\s*\(.*?\) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROMPT: | |
Write a REGEX expression to match the following WordPress i18n functions: | |
__ | |
_x | |
_n | |
_nx | |
PROMT: | |
Adjust the REGEX to support multiple functions per line and include an example in PHP. | |
PROMT: | |
Update the REGEX to find the function names in a JavaScript file. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment