0

I am building a search functionality and I am trying to make it similar to the one in Sublime Text.

Assume "cmd" as the input string and "command" is one of the results.

To search the files, among other things, I split that input by chars and end up with the following regex: c.*?m.*?d. This part is succesfull in finding files like "command", however, when I use the same regex to replace the found string with some HTML elements to evidentiate the fact that the searched string is found in that particular item, this results in something like this:

<span>command</span>

I understand exactly why this is happening and I'm looking for and alternative to display to the user something like the following:

<span>c</span>o<span>m</span><span>m</span>an<span>d</span>

Or, maybe just:

<span>c</span>o<span>m</span>man<span>d</span>

I have an idea of how to do this, which is by encapsulating every single character in between parantheses and then replace every single one with the <span>$x</span> part, but I'm not sure how to do this exactly.

Any kind of help is immensely appreciated. Thanks,

Gecko
  • 316
  • 3
  • 11
  • This problem is [an HTML parser job, not a regex](http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not). Choose the right tool for the problem. – m0skit0 Feb 14 '15 at 15:23
  • How is this an HTML parser problem? I am not parsing any HTML. I am generating it using regex. – Gecko Feb 14 '15 at 15:29
  • I have solved this problem using an algorithm and some dirty techniques. Not solely based on regex, though, but it does the trick. – Gecko Feb 14 '15 at 16:10
  • https://regex101.com/r/nS8gE8/2 ? – Avinash Raj Feb 14 '15 at 16:16

0 Answers0