JsonPath expression to filter using regex

前端 未结 3 901
失恋的感觉
失恋的感觉 2021-01-11 17:19

We are using a tool which uses jayway library for evaluating JSONpath expression. Javascript does NOT seem to work with it. How can I use regular expression

相关标签:
3条回答
  • 2021-01-11 17:23

    For the record, a workaround for conditional regex in Goessner's javascript JSONpath would be to write the query as follow:

    $.store.book[?(/^.*sword.*$/i.test(@.title))]
    

    Please see here https://github.com/jpaquit/jsonpath/tree/0.8.5-+-regexp for "=~" syntax in JS lib.

    0 讨论(0)
  • 2021-01-11 17:46

    You could use capturing group or lookbehind assertion.

    "title":\s*"([^"]*\bSword\b[^"]*)"
    

    Add case-insensitive modifier i if necessary. Grab the title string from group index 1.

    DEMO

    0 讨论(0)
  • 2021-01-11 17:47

    The Jayway implementation uses the Ruby regex operator:

    $.store.book[?(@.title =~ /^.*Sword.*$/)]
    

    To ignore case:

    $.store.book[?(@.title =~ /^.*sword.*$/i)]
    
    0 讨论(0)
提交回复
热议问题