Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Created May 31, 2023 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjohnson/e049ad666ecb63df17a4477c4b45d298 to your computer and use it in GitHub Desktop.
Save kjohnson/e049ad666ecb63df17a4477c4b45d298 to your computer and use it in GitHub Desktop.
REGEX for WordPress i18n functions in JavaScript
/* 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")'
// ]
(?:__|_x|_n|_nx)\s*\(.*?\)
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