Skip to content

Instantly share code, notes, and snippets.

@iwill
Last active March 19, 2024 13:40
Show Gist options
  • Save iwill/a1835fcd38efe6beb20e938e1f80f371 to your computer and use it in GitHub Desktop.
Save iwill/a1835fcd38efe6beb20e938e1f80f371 to your computer and use it in GitHub Desktop.
Make RegExp in multiple lines, and indent with white spaces
// #see https://stackoverflow.com/a/12317105/456536
RegExp.make = (regExp, opts) => new RegExp(regExp.raw[0].replace(/( #.*|\s)/gm, ""), opts || "")
@iwill
Copy link
Author

iwill commented Apr 6, 2022

Usage:

let reg = RegExp.make`
    (
        # 声母 + 韵母
        ([bpmf dtl k jqx yw]?|[zcs]?h?|[ngr]?)
            ([iu][ao](ng?)|(iao|uai|ve)|i[aue]|u[aoien]|[iuv])
        # 可选声母 + 可独立韵母
        | ([bpmf dtl k jqx yw]?|[zcs]?h?|[ngr]?)?
            ([aeio](ng?)|a[io]|(ei|ou)|[aoe])
        # 独立韵母
        | er
    )
    $
`

@iwill
Copy link
Author

iwill commented Apr 12, 2022

Ruby:

reg = /
    (
        # 声母 + 韵母
        ([bpmf dtl k jqx yw]?|[zcs]?h?|[ngr]?)
            ([iu][ao](ng?)|(iao|uai|ve)|i[aue]|u[aoien]|[iuv])
        # 可选声母 + 可独立使用的韵母
        |([bpmf dtl k jqx yw]?|[zcs]?h?|[ngr]?)?
            ([aeio](ng?)|a[io]|(ei|ou)|[aoe])
        # 只能独立使用的韵母
        |er
    )
    $
/x

@iwill
Copy link
Author

iwill commented Mar 19, 2024

Swift:

let reg2nd = #/
(
    # 声母 + 韵母
    ([bpmf dtl k jqx yw]?|[zcs]?h?|[ngr]?)
        ([iu][ao](ng?)|(iao|uai|ve)|i[aue]|u[aoien]|[iuv])
    # 可选声母 + 可独立使用的韵母
    | ([bpmf dtl k jqx yw]?|[zcs]?h?|[ngr]?)?
        ([aeio](ng?)|a[io]|(ei|ou)|[aoe])
    # 只能独立使用的韵母
    | er
)
$
/#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment