Skip to content

[New Feature]: Add regex escape utility or literal string replaceAll method #44505

@anjuchamantha

Description

@anjuchamantha

Description

Description

Ballerina currently lacks a built-in way to escape regex metacharacters when building regex patterns from user/config strings, or to perform literal string replacement without regex matching.

Use Case

When replacing literal strings (e.g., FHIR base URLs) in text, developers need to use re ${baseUrl}.replaceAll(...), but this treats baseUrl as a regex pattern. URLs contain metacharacters like . and ? which cause false-positive matches.

For example:

  • If baseUrl = https://fhir.example.com
  • The pattern re https://fhir.example.com`` treats . as "match any character"
  • This would incorrectly match https://fhirXexampleYcom

Current Limitations

  1. replaceAll() is only available on RegExp objects and always performs regex matching
  2. lang.regexp module doesn't expose an escape() function to escape metacharacters
  3. No lang.string.replaceAll() method for literal string replacement exists

Proposed Solutions

One of the following would address this:

  1. Add a regexp:escape(string) function that escapes regex metacharacters
  2. Add a string.replaceAll(literal, replacement) method for literal string replacement
  3. Document a recommended pattern for safely building regex from literal strings

Related Discussion

This issue was identified during code review of a FHIR URL rewriting utility:

Describe your problem(s)

No response

Describe your solution(s)

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions