MCPcopy Create free account
hub / github.com/dop251/goja / stringproto_replaceAll

Method stringproto_replaceAll

builtin_string.go:707–741  ·  view source on GitHub ↗
(call FunctionCall)

Source from the content-addressed store, hash-verified

705}
706
707func (r *Runtime) stringproto_replaceAll(call FunctionCall) Value {
708 r.checkObjectCoercible(call.This)
709 searchValue := call.Argument(0)
710 replaceValue := call.Argument(1)
711 if o, ok := searchValue.(*Object); ok {
712 if isRegexp(searchValue) {
713 flags := nilSafe(o.self.getStr("flags", nil))
714 r.checkObjectCoercible(flags)
715 if !strings.Contains(flags.toString().String(), "g") {
716 panic(r.NewTypeError("String.prototype.replaceAll called with a non-global RegExp argument"))
717 }
718 }
719 if replacer := toMethod(r.getV(searchValue, SymReplace)); replacer != nil {
720 return replacer(FunctionCall{
721 This: searchValue,
722 Arguments: []Value{call.This, replaceValue},
723 })
724 }
725 }
726
727 s := call.This.toString()
728 var found []regexpResult
729 searchStr := searchValue.toString()
730 searchLength := searchStr.Length()
731 advanceBy := toIntStrict(max(1, int64(searchLength)))
732
733 pos := s.index(searchStr, 0)
734 for pos != -1 {
735 found = append(found, regexpResult{indexes: []int{pos, pos + searchLength}})
736 pos = s.index(searchStr, pos+advanceBy)
737 }
738
739 str, rcall := getReplaceValue(replaceValue)
740 return r.stringReplace(s, found, str, rcall)
741}
742
743func (r *Runtime) stringproto_search(call FunctionCall) Value {
744 r.checkObjectCoercible(call.This)

Callers

nothing calls this directly

Calls 15

checkObjectCoercibleMethod · 0.95
NewTypeErrorMethod · 0.95
getVMethod · 0.95
stringReplaceMethod · 0.95
isRegexpFunction · 0.85
nilSafeFunction · 0.85
toMethodFunction · 0.85
toIntStrictFunction · 0.85
getReplaceValueFunction · 0.85
getStrMethod · 0.65
StringMethod · 0.65
toStringMethod · 0.65

Tested by

no test coverage detected