1

I want to target or match only the first occurrence per line

Typical Scenario:

I have an HTML Structure that I am using in JavaScript.

<ul>
    <li> ABC </li>
    <li> DEF </li>
    <li> GHI <span> GHI-SPAAN </span> </li>
</ul>

To convert the above into a string, in my editor, I can simply Find & replace EOL with a '+ and beginning of line with a ' so that the code would be

var tpl = ''+
    '<ul> '+
    '<li> ABC </li> '+
    '<li> DEF </li> '+
    '<li> GHI <span> GHI-SPAAN </span> </li> '+
    '</ul> ';

But you see, I loose the indentation when I replace the beginning of line with '<

So I want to uniquely target (target only the first occurrence of < and replace with '< )

I am using KOMODO edit and Sublime Text 2

4
  • 1
    What is wrong with .innerHTML? Commented Apr 25, 2012 at 6:16
  • 1
    Who cares about the indentation of markup that is inside a string anyway? Commented Apr 25, 2012 at 6:25
  • @Blender, Oops! Sorry for not being clear. I want to store some HTML in a variable (not taken from DOM, I am gonna create it). I want to copy paste it from some HTML file and save myself, the typing efforts Commented Apr 30, 2012 at 17:06
  • @ThiefMaster, I want to keep the indentation so that it looks good to the one who reads code, and follow the standard Commented Apr 30, 2012 at 17:15

2 Answers 2

1

I'm not a KomodoEdit user, but I tried these replacements and they worked:

  • starting quote: replace ^(\s*)< with \1<
  • ending quote: replace >\s*$ with >' +

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

The \1 was what I was looking for - It keeps your matches. Thanks for showing understanding ability - he he
0

Rather than converting HTML to a JS string, it would be much better to create the elements in JS and put them in the DOM. This would give you much more control, not create such a difficult to maintain/read code, and be much faster (amongst other benefits):

var outerDiv = document.createElement("div");
outerDiv.className = "spa-shell-head";

var innerDivLogo = document.createElement("div");
innerDivLogo.className = "spa-shell-head-logo";

var innerDivAcct = document.createElement("div");
innerDivAcct.className = "spa-shell-head-acct";

var innerDivSearch = document.createElement("div");
innerDivSearch.className = "spa-shell-head-search";

outerDiv.appendChild(innerDivLogo);
outerDiv.appendChild(innerDivAcct);
outerDiv.appendChild(innerDivSearch);
document.body.appendChild(outerDiv);

The above creates the following:

enter image description here

https://jsfiddle.net/yfeLbhe4/

5 Comments

That was only an example, the main question was to maintain indentation.
@OmShankar Why would you wish to maintain indentation?
Single page apps, templates. Scala map methods. XML fragments, YAML configuration, etc.
@OmShankar Why not write something that reformats the code before it's displayed? Much less likely to produce errors in formatting if it's not left up to the user.
you didn't get my comment. I just wanted to say that in a normal day to day coding (including back end) you need indentation, not just HTML files. Now if you do find-and-replace like the example I gave above, you would loose indentation in the code file. So there should be a way to maintain the formatting of any code after such operation. Which is something I found in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.