regexstringreplacecasetypst

How to convert only the first letter as uppercase?


So, I want to convert only the first letter using REGEX and then bypassing function to uppercase it. However Typst return "expected string or content found dictionary"

This is the source:

#let text="oh this is it"
#text.replace(regex("^\w"), m=>upper(m))

and oh, If you have insight on custom function as a callback in Typst I would be happy as I really struggle to find good documentation on it.

thaks

I tried that code and I expect the first char to be upprcased but didnt work


Solution

  • So, finally I can resolve this.

    While Typst scripting looks like JS, the implementation detail is bit confusing as the docs missing this kind of detail.

    This expression will return expected string or content found dictionary

    #text.replace(regex("^\w"), m=>upper(m))

    the problem lies at m=>upper(m). So, Typst treat the m matched captured as Dictionary rather than direct value being captured, while upper() expect its param to be string or content.

    The fix is simple.

    full fixed code:

    #let text="oh this is it"
    #text.replace(regex("^\w"), m=>{
    repr(m)
    upper(m.text)
    })
    

    EDIT: you can also use the shorter version without repr in just one liner fx:

    #text.replace(regex("^\w"), m=>upper(m))