Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:48:49+00:00 2026-06-07T08:48:49+00:00

What I’m trying to do is open up a file and search for searchText.

  • 0

What I’m trying to do is open up a file and search for “searchText”. I want to replace all the instances of it in the file with a new link, which is actually just the filename with an achor link so instead of opening up javascript it just goes to another point in the page.

So far what I have is this:

private void writeNotes(){
        StreamReader reader = new StreamReader(openFileDialog1.FileName);
        string content = reader.ReadToEnd();

        reader.Close();

        string fileName = openFileDialog1.SafeFileName;
        string searchText = "<a class=\"x-fn\" href=\"javascript:void(0);\">";
        string replaceText = "<a class=\"x-fn\" href=\"" + fileName + "#fn" + "\">";

        content = Regex.Replace(content, searchText, replaceText);

        StreamWriter writer = new StreamWriter(openFileDialog1.FileName);
        writer.Write(content);
        writer.Close();

However, after writing and closing… I open up the file and no changes were made. Besides that, what I want to do is add a number that counts up after “#fn” for every instance of replacement. So, basically, for every time I replace the javascript link with another, I want it to be:

<a class="x-fn" href="fileName#fn1">

And then when I replace the second instance of javascript, it reads

<a class="x-fn" href="fileName#fn2">

and so on…

I imagine I would have to count the instances of how many times the javascript appears, replace it, and use a for loop to iterate throughout all of the new links and add the #fn(n) at the end?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:48:51+00:00Added an answer on June 7, 2026 at 8:48 am

    Your problem is that you have metacharacters (in this case, the open and close paren in “void(0)”). Instead of representing literal open and close parens, that is creating a regular expression group, which is causing your match to fail. If you escape the parens with a backslash, it will work as expected.

    However, since you’re just matching a string literal, you don’t need to use regular expressions at all; it’ll be faster to use string.Replace.

    As for the second part of your question, that’s a little trickier. There’s no easy way to do it that I know of, so the best approach is to look for your search text in the input, and then build up a StringBuilder as you go along, incrementing a count variable. In the following example, the word “the” is replaced by “(0)” and “(1)” for simplicity’s sake, but you can adapt it to your problem easily enough.

    var content = "the quick brown fox jumped over the lazy dog";
    var searchText = "the";
    var sb = new StringBuilder();
    var idx = -1; // will hold the index of our search text
    var uncopiedIdx = 0; // the start index of what hasn't been copied yet
    var replacementCount = 0;
    while( (idx = content.IndexOf( searchText, idx+1 )) != -1 ) {
        // copy everything leading up to our search text
        sb.Append( content.Substring( uncopiedIdx, idx-uncopiedIdx ) );
        // copy the replacement text, with the replacement count
        var replacement = "(" + replacementCount++ + ")";
        sb.Append( replacement );
        // skip over the search text
        uncopiedIdx = idx + searchText.Length;
    }
    // copy everything after the last match
    sb.Append( content.Substring( uncopiedIdx ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to select an H1 element which is the second-child in its group
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.