Results 1 to 5 of 5

Thread: [RESOLVED] Reg Ex to get anchor name

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Reg Ex to get anchor name

    I have anchor tags which are unusually formatted.

    I firstly I have tags which are as below <a name="1">text</a>

    I need to get the value from the name attribute.

    I then need to get the whole 1st part of the tag <a name="1"> so I can replace it with a properly formated tag i.e. <a href="URL" name="1">

    I am a little lost in the world of Reg Ex and all the examples presume that there is a "href" followed by a formatted URL present.

    String p18 = @"/<a[^>]*?name=[""""][^>]*?";

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Reg Ex to get anchor name

    judging from the last line of your post, are you using C# or something?
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Reg Ex to get anchor name

    Yes im using asp.net server side c# to get a string out of the ajax toolkit html editor then parse out and replace values in the html tags

  4. #4
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Reg Ex to get anchor name

    https://developer.mozilla.org/en/DOM...ElementsByName
    javascript Code:
    1. document.getElementsByName

    https://developer.mozilla.org/en/DOM...t.setAttribute
    javascript Code:
    1. element.setAttribute


    You can do it without regex. Use document.getElementsByName to find the element, and then add the href attribute with element.setAttribute.

    EDIT: above stuff is for client-side JavaScript. Server-side code could do it with some XML stuff (XPath, etc.) but I'm not an expert on that stuff.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  5. #5

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Reg Ex to get anchor name

    Thanks the HTML agility pack made it quite painless although it did take some figuring
    Code:
    String parsedtext;
                parsedtext = OutlineText.Replace("shape=\"rect\"", "");
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(Text);
                HtmlNodeCollection nc = doc.DocumentNode.SelectNodes("//a[@name]");
                if (nc != null)
                {
                    foreach (HtmlNode link in nc)
                    {
                        if (!link.Attributes.Contains("href"))
                        {
                            HtmlAttribute name = link.Attributes["name"];
                            Corp.Web.GSP.Model.Link objLink;
                            objLink = objLinks.FindById(Convert.ToInt64(name.Value));
                            if (objLink != null)
                            {
    
                                link.Attributes.Add("href", objLink.URLRef.ToString());
    
                                doc.DocumentNode.WriteTo();
                            }
                        }
                    }
                    parsedtext = doc.DocumentNode.InnerHtml.ToString();
                }
    
                return parsedtext;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width