package melange-str

  1. Overview
  2. Docs
Str module for Melange

Install

dune-project
 Dependency

Authors

Maintainers

Sources

melange-str-0.1.0.tbz
sha256=98f6816eb24009a45cf02b853633c1e66ebb71524649f1b3239c7160f6a3869e
sha512=b62348a96af875a6041b224657fbb8b8dda00b409ac27e0bdfdbd10a21ca283ef8163f472ec871964c174374afc21896d4a870bd6e4578c36045de7fd72c55f8

Description

Implementation of Str module for Melange, using JavaScript's RegExp as the backend.

Published: 06 Mar 2026

README

melange-str

Implementation of OCaml's Str module for Melange, using JavaScript's RegExp as the backend.

Installation

opam install melange-str

Then add melange-str to the libraries field in your dune file:

(library
 (name my_lib)
 (modes melange)
 (libraries melange-str)
 (preprocess (pps melange.ppx)))

Basic matching

let r = Str.regexp {|hello \([A-Za-z]+\)|}
let matched = Str.string_match r "hello world" 0
(* true *)

let group = Str.matched_group 1 "hello world"
(* "world" *)

Search and replace

let r = Str.regexp {|foo|}
Str.global_replace r "bar" "foo foo foo"
(* "bar bar bar" *)

let r = Str.regexp {|\([a-z]+\) \([a-z]+\)|}
Str.global_replace r {|\2 \1|} "hello world"
(* "world hello" *)

Split

let r = Str.regexp {| +|}
Str.split r "hello   world   test"
(* ["hello"; "world"; "test"] *)

let r = Str.regexp {|[,;]|}
Str.full_split r "a,b;c"
(* [Text "a"; Delim ","; Text "b"; Delim ";"; Text "c"] *)

Advanced features

let r = Str.regexp_case_fold {|hello|}
Str.string_match r "HELLO" 0  (* true *)

let r = Str.regexp {|[0-9]+|}
Str.search_backward r "abc 123 def 456 ghi" 10
(* 4 (position of "123") *)

let r = Str.regexp {|[0-9]+|}
Str.global_substitute r
  (fun s ->
    let n = int_of_string (Str.matched_string s) in
    string_of_int (n * 2))
  "a 10 b 20 c"
(* "a 20 b 40 c" *)

Universal code (Melange + Native)

If you have code that uses Str and needs to compile for both Melange (JavaScript) and native OCaml, you can use dune's copy_files to share source files between both targets. The Melange side depends on melange-str while the native side depends on OCaml's str.

We recommend placing shared source files inside the native folder. The native library uses them directly, and the Melange library copies them in via copy_files. This way the native side is the "source of truth" and doesn't need any extra dune stanzas.

server/
  shared/
    input.ml        <- your code that uses Str
  dune              <- native library
client/
  dune              <- melange library (copies from server/shared)

server/shared/input.ml:

let parse_csv line =
  let r = Str.regexp {|,|} in
  Str.split r line

let replace_dates text =
  let r = Str.regexp {|\([0-9]+\)/\([0-9]+\)/\([0-9]+\)|} in
  Str.global_replace r {|\3-\2-\1|} text

server/dune (Native):

(library
 (name my_parser_native)
 (modes native)
 (libraries str)
 (wrapped false))

client/dune (Melange):

(library
 (name my_parser_js)
 (modes melange)
 (libraries melange-str)
 (wrapped false)
 (preprocess (pps melange.ppx)))

(copy_files#
 (source_only)
 (mode fallback)
 (files "../server/shared/*.ml"))

Both libraries expose the same modules, so the rest of your code can depend on my_parser_js or my_parser_native depending on the target. The mode fallback option lets you override a shared file in client/ if you need Melange-specific behavior.

For more details on organising universal code, see How to organise universal code from server-reason-react documentation.

Known Limitations

  • Partial match isn't feature complete and may diverge from native Str on complex regex edge cases.

Dependencies (3)

  1. melange >= "6.0"
  2. ocaml >= "5.0"
  3. dune >= "3.8"

Dev Dependencies (4)

  1. odoc with-doc
  2. ocamlformat with-dev-setup
  3. ocaml-lsp-server with-dev-setup
  4. conf-npm with-test

Used by

None

Conflicts

None