V
V
v- death2015-12-10 01:25:44
JavaScript
v- death, 2015-12-10 01:25:44

How to rewrite regex from js to golang?

Hello. js has regular expressions

var key = "test";
html = html.replace(new RegExp("{{"+key+"}}","g"), key);
if_array = html.match(/<if>(.*?)<\/if>/g);

which replaces all {{test}} with test and then <if></if>puts all the data between the tags into an array [data1, data2, data3]
I have been suffering for several days now... Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HomeEdition, 2015-12-10
@vGrabko99

package main

import (
  "fmt"
  "regexp"
)

func main() {
  html := "{{test}}ing <if>abc</if> {{test}} <if>123</if>"
  key := "test"
  html = regexp.MustCompile("{{"+key+"}}").ReplaceAllString(html, key)
  array_temp := regexp.MustCompile("<if>(.*?)</if>").FindAllStringSubmatch(html, -1)
  
  var if_array []string
  
  for _, val := range array_temp {
    if_array = append(if_array, val[1])
  }

  fmt.Println(html)
  fmt.Println(if_array)
}

S
Stalker_RED, 2015-12-10
@Stalker_RED

But... there are two regulars!
https://regex101.com/r/uG5bA6/1
https://regex101.com/r/uG5bA6/2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question