fromCharCode n creates a string containing the character corresponding to that number; n ranges from 0 to 65535. If out of range, the lower 16 bits of the value are used. Thus, fromCharCode 0x1F63A gives the same result as fromCharCode 0xF63A.
fromCodePoint n creates a string containing the character corresponding to that numeric code point. If the number is not a valid code point, raisesRangeError. Thus, fromCodePoint 0x1F63A will produce a correct value, unlike fromCharCode 0x1F63A, and fromCodePoint -5 will raise a RangeError.
fromCharCodeMany [|n1;n2;n3|] creates a string from the characters corresponding to the given code point numbers, using the same rules as fromCodePoint.
get s n returns as a string the character at the given index number. If n is out of range, this function returns undefined, so at some point this function may be modified to return t option.
get "Reason" 0 = "R";;
get "Reason" 4 = "o";;
get {js|Rẽasöń|js} 5 = {js|ń|js};;
charAt n s gets the character at index n within string s. If n is negative or greater than the length of s, returns the empty string. If the string contains characters outside the range \u0000-\uffff, it will return the first 16-bit value at that position in the string.
charCodeAt n s returns the character code at position n in string s; the result is in the range 0-65535, unlke codePointAt, so it will not work correctly for characters with code points greater than or equal to 0x10000. The return type is float because this function returns NaN if n is less than zero or greater than the length of the string.
codePointAt n s returns the code point at position n within string s as a Some value. The return value handles code points greater than or equal to 0x10000. If there is no code point at the given position, the function returns None.
endsWithFrom ending len str returns true if the first len characters of str end with ending, false otherwise. If n is greater than or equal to the length of str, then it works like endsWith. (Honestly, this should have been named endsWithAt, but oh well.)
includes searchValue start s returns true if searchValue is found anywhere within s starting at character number start (where 0 is the first character), false otherwise.
indexOfFrom searchValue start s returns the position at which searchValue was found within s starting at character position start, or -1 if searchValue is not found in that portion of s. The return value is relative to the beginning of the string, no matter where the search started from.
lastIndexOf searchValue s returns the position of the last occurrence of searchValue within s, searching backwards from the end of the string. Returns -1 if searchValue is not in s. The return value is always relative to the beginning of the string.
lastIndexOfFrom searchValue start s returns the position of the last occurrence of searchValue within s, searching backwards from the given start position. Returns -1 if searchValue is not in s. The return value is always relative to the beginning of the string.
val match_ : Js__.Js_re.t->t->t option array option
match regexp str matches a string against the given regexp. If there is no match, it returns None. For regular expressions without the g modifier, if there is a match, the return value is Some array where the array contains:
The entire matched string
Any capture groups if the regexp had parentheses
For regular expressions with the g modifier, a matched expression returns Some array with all the matched substrings and no capture groups.
match [%re "/b[aeiou]t/"] "The better bats" = Some [|"bet"|]
match [%re "/b[aeiou]t/g"] "The better bats" = Some [|"bet";"bat"|]
match [%re "/(\\d+)-(\\d+)-(\\d+)/"] "Today is 2018-04-05." =
Some [|"2018-04-05"; "2018"; "04"; "05"|]
match [%re "/b[aeiou]g/"] "The large container." = None
normalize str returns the normalized Unicode string using Normalization Form Canonical (NFC) Composition.
Consider the character ã, which can be represented as the single codepoint \u00e3 or the combination of a lower case letter A \u0061 and a combining tilde \u0303. Normalization ensures that both can be stored in an equivalent binary representation.
replace substr newSubstr string returns a new string which is identical to string except with the first matching instance of substr replaced by newSubstr.
substr is treated as a verbatim string to match, not a regular expression.
replace "old" "new" "old string" = "new string"
replace "the" "this" "the cat and the dog" = "this cat and the dog"
val unsafeReplaceBy0 : Js__.Js_re.t->(t->int ->t->t)->t->t
returns a new string with some or all matches of a pattern with no capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the offset at which the match begins, and the whole string being matched
let str = "beautiful vowels"
let re = [%re "/[aeiou]/g"]
let matchFn matchPart offset wholeString =
Js.String.toUpperCase matchPart
let replaced = Js.String.unsafeReplaceBy0 re matchFn str
let () = Js.log replaced (* prints "bEAUtifUl vOwEls" *)
val unsafeReplaceBy1 : Js__.Js_re.t->(t->t->int ->t->t)->t->t
returns a new string with some or all matches of a pattern with one set of capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the captured string, the offset at which the match begins, and the whole string being matched.
let str = "increment 23"
let re = [%re "/increment (\\d+)/g"]
let matchFn matchPart p1 offset wholeString =
wholeString ^ " is " ^ (string_of_int ((int_of_string p1) + 1))
let replaced = Js.String.unsafeReplaceBy1 re matchFn str
let () = Js.log replaced (* prints "increment 23 is 24" *)
val unsafeReplaceBy2 : Js__.Js_re.t->(t->t->t->int ->t->t)->t->t
returns a new string with some or all matches of a pattern with two sets of capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the captured strings, the offset at which the match begins, and the whole string being matched.
let str = "7 times 6"
let re = [%re "/(\\d+) times (\\d+)/"]
let matchFn matchPart p1 p2 offset wholeString =
string_of_int ((int_of_string p1) * (int_of_string p2))
let replaced = Js.String.unsafeReplaceBy2 re matchFn str
let () = Js.log replaced (* prints "42" *)
val unsafeReplaceBy3 :
Js__.Js_re.t->(t->t->t->t->int ->t->t)->t->t
returns a new string with some or all matches of a pattern with three sets of capturing parentheses replaced by the value returned from the given function. The function receives as its parameters the matched string, the captured strings, the offset at which the match begins, and the whole string being matched.
splitAtMost delimiter ~limit: n str splits the given str at every occurrence of delimiter and returns an array of the first n resulting substrings. If n is negative or greater than the number of substrings, the array will contain all the substrings.
splitByRe regex str splits the given str at every occurrence of regex and returns an array of the resulting substrings.
splitByRe [%re "/\\s*[,;]\\s*/"] "art; bed , cog ;dad" = [|Some "art"; Some "bed"; Some "cog"; Some "dad"|];;
splitByRe [%re "/[,;]/"] "has:no:match" = [|Some "has:no:match"|];;
splitByRe [%re "/(#)(:)?/"] "a#b#:c" = [|Some "a"; Some "#"; None; Some "b"; Some "#"; Some ":"; Some "c"|];;
;
val splitByReAtMost : Js__.Js_re.t->limit:int ->t->t option array
splitByReAtMost regex ~limit: n str splits the given str at every occurrence of regex and returns an array of the first n resulting substrings. If n is negative or greater than the number of substrings, the array will contain all the substrings.
splitByReAtMost [%re "/\\s*:\\s*/"] ~limit: 3 "one: two: three: four" = [|Some "one"; Some "two"; Some "three"|];;
splitByReAtMost [%re "/\\s*:\\s*/"] ~limit: 0 "one: two: three: four" = [| |];;
splitByReAtMost [%re "/\\s*:\\s*/"] ~limit: 8 "one: two: three: four" = [|Some "one"; Some "two"; Some "three"; Some "four"|];;
splitByReAtMost [%re "/(#)(:)?/"] ~limit:3 "a#b#:c" = [|Some "a"; Some "#"; None|];;
;
val splitRegexpLimited : Js__.Js_re.t->int ->t->t array
ES2015: startsWithFrom substr n str returns true if the str starts with substr starting at position n, false otherwise. If n is negative, the search starts at the beginning of str.
toLowerCase str converts str to lower case using the locale-insensitive case mappings in the Unicode Character Database. Notice that the conversion can give different results depending upon context, for example with the Greek letter sigma, which has two different lower case forms when it is the last character in a string or not.
toUpperCase str converts str to upper case using the locale-insensitive case mappings in the Unicode Character Database. Notice that the conversion can expand the number of letters in the result; for example the German ß capitalizes to two Ses in a row.