Skip to content

Instantly share code, notes, and snippets.

@Hugoberry
Last active June 16, 2023 07:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hugoberry/4948d96b45d6799c47b4b9fa1b08eadf to your computer and use it in GitHub Desktop.
Save Hugoberry/4948d96b45d6799c47b4b9fa1b08eadf to your computer and use it in GitHub Desktop.
let
fx=(x,y)=>
Web.Page(
"<script>
var x="&x&";
var y=new RegExp('"&y&"','g');
var b=x.match(y);
document.write(b);
</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}
in
fx("""hello012中国1235""","\\d+")
@SamWoolerton
Copy link

You can shrink down the whole thing above to just
(text, pattern) => Web.Page("<script>document.write(new RegExp('"& pattern &"').test('"& text &"'))</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}

I also recommend using this as a function that you call separately, as it'll keep things way cleaner

@szebrowski
Copy link

You can shrink down the whole thing above to just
(text, pattern) => Web.Page("<script>document.write(new RegExp('"& pattern &"').test('"& text &"'))</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}

this only gets you a true/false output

@szebrowski
Copy link

szebrowski commented Feb 2, 2020

My take on the snippet, ready to paste to as new query:

let   fx=(text,regex)=>
    Web.Page(
        "<script>
            var x='"&text&"';
            var y=new RegExp('"&regex&"','g');
            var b=x.match(y);
            document.write(b);
        </script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}

in
fx

basically it's the same, it's only easier to use if you don't know power query :)

edit: check the performance of this solution, I think this it is super slow for bigger data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment