9951 explained code solutions for 126 technologies


nginx-luaHow to use regex


server {
  location / {
    content_by_lua_block {
      local str = 'Hi, Donald! Do you like Joe?';
      local n1, n2 = str:match('(D.+\\?)!.+(J.+)?')
      
      ngx.say(n1)
      ngx.say(n2)
    }
  }
}ctrl + c
content_by_lua_block

nginx-lua module directive to specify block of Lua code

local str

sample string to run regex on

n1, n2

these variables will store captured groups from matches

str:match

run regex expression on str

ngx.say

output given text to client