regex


Node.js

  1. let input = 'foobar'
  2. let replaced = input.replace(/foo(.*)/i, 'qux$1')
  3. console.log(replaced)
  4. let match = /o{2}/i.test(input)
  5. console.log(match)
  6. input = '111-222-333'
  7. let matches = input.match(/([0-9]+)/gi)
  8. console.log(matches)

Output

  1. quxbar
  2. true
  3. [ '111', '222', '333' ]

Go

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func main() {
  7. input := "foobar"
  8. re := regexp.MustCompile(`(?i)foo(.*)`)
  9. replaced := re.ReplaceAllString(input, "qux$1")
  10. fmt.Println(replaced)
  11. re = regexp.MustCompile(`(?i)o{2}`)
  12. match := re.Match([]byte(input))
  13. fmt.Println(match)
  14. input = "111-222-333"
  15. re = regexp.MustCompile(`(?i)([0-9]+)`)
  16. matches := re.FindAllString(input, -1)
  17. fmt.Println(matches)
  18. }

Output

  1. quxbar
  2. true
  3. [111 222 333]