简体   繁体   中英

Regex to match string between two words, where the ending boundary word is optional

I'm trying to match text between the words called and width but also still match even if width is missing. eg

In this sentence, I want to match Mary Jane

"Create a text field called Mary Jane with ninja"

Here I want still to match Mary Jane, even without a with clause afterwards

Create a text field called Mary Jane .

My regex only matches when with is present, but not if it is absent.

"Create a picklist called Mary Jane with the value ox'".match(/called(.*)(?:with)/i) // Matches "Mary Jane"

"Create a picklist called Mary Jane'".match(/called(.*)(?:with)/i) // Error: Does not match anything

How can I write a regex that can match both cases?

To match the name without the leading spaces and without the dot, you might use a capturing group with an alternation :

\bcalled (.*?)(?: with|\.?$)
  • \\bcalled Match literally preceded with a word boundary
  • (.*?) Capture group 1, match any char except a newline non greedy
  • (?: Non capturing group
    • with Match literally
    • | Or
    • \\.?$ Match an optional dot and assert end of the string
  • ) Close non capturing group

Regex demo

You can do it like this. This is what I came up with. I hope it helps.

 let str1 = "Create a text field called Mary Jane with ninja"; let str2 = "Create a text field called Mary Jane"; console.log(words(str1)); console.log(words(str2)); function words(str){ let arr = str.split(" "); let i = arr.indexOf("called"); let j = arr.indexOf("with"); if( j == -1){ return arr.slice(i+1); } return arr.slice(i+1,j); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM