public override bool TryIteratorStep(out ObjectInstance nextItem) { if (_done) { nextItem = CreateIterResultObject(Undefined, true); return(false); } var match = RegExpPrototype.RegExpExec(_iteratingRegExp, _s); if (match.IsNull()) { _done = true; nextItem = CreateIterResultObject(Undefined, true); return(false); } if (_global) { var macthStr = TypeConverter.ToString(match.Get(JsString.NumberZeroString)); if (macthStr == "") { var thisIndex = TypeConverter.ToLength(_iteratingRegExp.Get(RegExpInstance.PropertyLastIndex)); var nextIndex = thisIndex + 1; _iteratingRegExp.Set(RegExpInstance.PropertyLastIndex, nextIndex, true); } } else { _done = true; } nextItem = CreateIterResultObject(match, false); return(false); }
private JsValue Replace(JsValue thisObj, JsValue[] arguments) { TypeConverter.CheckObjectCoercible(Engine, thisObj); var searchValue = arguments.At(0); var replaceValue = arguments.At(1); if (!searchValue.IsNullOrUndefined()) { var replacer = GetMethod(_engine, searchValue, GlobalSymbolRegistry.Replace); if (replacer != null) { return(replacer.Call(searchValue, new[] { thisObj, replaceValue })); } } var thisString = TypeConverter.ToJsString(thisObj); var searchString = TypeConverter.ToString(searchValue); var functionalReplace = replaceValue is ICallable; if (!functionalReplace) { replaceValue = TypeConverter.ToJsString(replaceValue); } var pos = thisString.IndexOf(searchString, StringComparison.Ordinal); var matched = searchString; if (pos < 0) { return(thisString); } string replStr; if (functionalReplace) { var replValue = ((ICallable)replaceValue).Call(Undefined, new JsValue[] { matched, pos, thisString }); replStr = TypeConverter.ToString(replValue); } else { var captures = new string[0]; replStr = RegExpPrototype.GetSubstitution(matched, thisString.ToString(), pos, captures, Undefined, TypeConverter.ToString(replaceValue)); } var tailPos = pos + matched.Length; var newString = thisString.Substring(0, pos) + replStr + thisString.Substring(tailPos); return(newString); }
public string MatchEvaluator(Match m) { return(RegExpPrototype.InvokeReplacementCallback(replacement, new MatchResult(re, input, m, null, 0))); }
/// <summary> /// Replaces occurences of substrings that matches the pattern by the value returned from the invocation of pipe function argument. /// </summary> /// <param name="input">Input string.</param> /// <param name="replacement">A pipe function argument.</param> /// <returns></returns> public string Replace(string input, RuntimeObject replacement) { return(ReplaceInternal(input, m => RegExpPrototype.InvokeReplacementCallback(replacement, new MatchResult(this, input, m, null, 0)))); }