|
-
May 20th, 2011, 03:31 AM
#1
Thread Starter
Frenzied Member
[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=[""""][^>]*?";
-
May 21st, 2011, 12:17 PM
#2
Re: Reg Ex to get anchor name
judging from the last line of your post, are you using C# or something?
-
May 22nd, 2011, 05:55 AM
#3
Thread Starter
Frenzied Member
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
-
May 22nd, 2011, 07:38 AM
#4
Re: Reg Ex to get anchor name
https://developer.mozilla.org/en/DOM...ElementsByName
javascript Code:
document.getElementsByName
https://developer.mozilla.org/en/DOM...t.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.
-
May 23rd, 2011, 05:59 AM
#5
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|