Skip to content

Instantly share code, notes, and snippets.

@Hugoberry
Last active November 24, 2024 23:14
Show Gist options
  • Select an option

  • Save Hugoberry/4948d96b45d6799c47b4b9fa1b08eadf to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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
Copy Markdown

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

szebrowski commented Feb 2, 2020

Copy link
Copy Markdown

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