Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Replacing a character in $args

I want to replace each + with (space) in $args.

I know the solution of

if ($args ~ ^(.*)\+(.*)$) {
  return 301 "$uri?$1 $2";
}

However that's not ideal because there's a redirect for each +. Is there a better solution?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could use more than one regular expression. Each regular expression tests the $args parameter for one, two, three, etc. + characters (evaluated in reverse order) which will minimise the recursion.

A compact solution uses a map to define the regular expressions.

For example:

map $args $spaces {
    default 0;
    ~^(.*)\+(.*)\+(.*)\+(.*)\+(.*)$  "$1 $2 $3 $4 $5";
    ~^(.*)\+(.*)\+(.*)\+(.*)$        "$1 $2 $3 $4";
    ~^(.*)\+(.*)\+(.*)$              "$1 $2 $3";
    ~^(.*)\+(.*)$                    "$1 $2";
}
server {
    ...
    if ($spaces) { return 301 "$uri?$spaces"; }
    ...
}

You can use more or less regular expressions to optimise the solution. The above will generate one redirection for up to four + characters, and two redirections for between five and eight + characters, and so on.

See this document for details.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!