Thanks to @juancarlospaco, we now have [Regular Expressions for the JavaScript 
target](https://nim-lang.github.io/Nim/jsre.html).

In `jsre` module above the runtime regex constructor is supported but the 
compile time constructor is not. Compile time constructor in javascript looks 
like this:
    
    
    let re = /ab+c/;
    
    
    Run

My experiments:

**jsrex1.nim**
    
    
    import jsre
    
    func re*(pattern: cstring): RegExp {.importjs: "/#/".}
    
    let re1 = re"abc"  # in js: var re1 = /"abc"/
    
    echo re1.test("abcde")  # should be true but it is false
    
    
    Run

compiles, runs and gives wrong results since it builds the wrong regex 
(`/"abc"/` instead of `/abc/`).

**jsrex2.nim**
    
    
    import jsre
    
    template re*(pattern: string) = {.emit: ["""/""", pattern, """/"""] .}
    
    let re1: RegExp = re"abc"  # Error: expression ' {.emit: ["""/""", r"abc", 
"""/"""].}' has no type (or is ambiguous)
    
    
    Run

does not compile since emit section does not have a type.

**jsrex3.nim**
    
    
    import jsre
    
    func re*(pattern: cstring): RegExp {.importjs: "/$#/".}
    
    let re1 = re"abc"  # in js: var re1_623017 = "abc"./re/();
    # it will give SyntaxError in Javascript
    
    
    Run

compiles but raises syntaxerror in JS. I did not expect this to work but I was 
a bit surprised by the results in JS code.

Is there a way to make this work?

Reply via email to