Ejemplo n.º 1
0
        public RegExpArray exec(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            string value = this.lastIndex > 0 ? input.Substring(this.lastIndex) : input;
            Match  match = this.Match(value);

            if (this.global)
            {
                this.lastIndex = match.Success ? match.Index + match.Length : 0;
            }
            if (match.Success)
            {
                RegExpArray ret = new RegExpArray();
                foreach (Group g in match.Groups)
                {
                    if (g.Success)
                    {
                        ret.Add(g.Value);
                    }
                    else
                    {
                        ret.Add(null);
                    }
                }
                ret.input = input;
                ret.index = match.Index;
                return(ret);
            }

            return(null);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void MatchTest()
        {
            String      text   = "table football, foosball";
            RegExp      regex  = new RegExp("foo*");
            RegExpArray result = text.match(regex);

            Assert.AreEqual <int>(1, (int)result.length);
            Assert.AreEqual <string>("foo", result[0]);

            regex  = new RegExp("foo*", "g");
            result = text.match(regex);
            Assert.AreEqual <int>(2, (int)result.length);
            Assert.AreEqual <string>("foo", result[0]);
            Assert.AreEqual <string>("foo", result[1]);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        public new static Number parseInt(String value, Number fromBase = null)
        {
            try
            {
                if (fromBase != null && fromBase != 10)
                {
                    return(Convert.ToInt32(value, (int)fromBase));
                }

                RegExpArray matchResult = value.match("^([\\s-+0-9]+).*");
                if (matchResult == null)
                {
                    return(NaN);
                }
                else
                {
                    return(int.Parse(matchResult[1]));
                }
            }
            catch
            {
                return(NaN);
            }
        }