Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="regex"></param>
        /// <returns></returns>
        public RegExpArray match(RegExp regex)
        {
            this.CheckUndefined();

            string input = GetText(this);

            if (!regex.IsMatch(input))
            {
                return(null);
            }

            RegExpArray ret = new RegExpArray();

            if (regex.global)
            {
                foreach (Match match in regex.Matches(input))
                {
                    ret.Add(match.Value);
                }
            }
            else
            {
                Match match = regex.Match(input);
                foreach (Group g in match.Groups)
                {
                    ret.Add(g.Value);
                }
                ret.input = input;
                ret.index = match.Index;
            }

            return(ret);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        public String replace(RegExp regex, Func <String, String, String, String, String> func)
        {
            this.CheckUndefined();

            string text = GetText(this);

            if (!regex.IsMatch(text))
            {
                return(this);
            }

            if (regex.global)
            {
                string result = "";
                int    index  = 0;
                foreach (Match match in regex.Matches(text))
                {
                    GroupCollection groups      = match.Groups;
                    string          replacement = func(groups[0].Value, groups[1].Value, groups[2].Value, groups[3].Value);
                    result += (text.Substring(index, match.Index - index) + match.Result(replacement));
                    index   = match.Index + match.Length;
                }
                result += text.Substring(index);
                return(result);
            }
            else
            {
                Match           match       = regex.Match(text);
                GroupCollection groups      = match.Groups;
                string          replacement = func(groups[0].Value, groups[1].Value, groups[2].Value, groups[3].Value);
                return(text.Substring(0, match.Index) + match.Result(replacement) + text.Substring(match.Index + match.Length));
            }
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        public Number search(RegExp regex)
        {
            this.CheckUndefined();

            string text  = GetText(this);
            Match  match = regex.Match(text);

            if (match.Success)
            {
                return(match.Index);
            }
            return(-1);
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public RegExpArray match(string pattern)
        {
            this.CheckUndefined();

            string input = GetText(this);

            if (!RegExp.IsMatch(input, pattern))
            {
                return(null);
            }

            RegExpArray ret   = new RegExpArray();
            Match       match = RegExp.Match(input, pattern);

            foreach (Group g in match.Groups)
            {
                ret.push(g.Value);
            }
            ret.input = input;
            ret.index = match.Index;

            return(ret);
        }