Insert multiple line breaks into a JavaScript string (regex) (CodeMirror) - javascript

I have a few strings and I would like to insert some line breaks into them at certain points.
I figured out a few of the logistics but as a whole I can't seem to crack this problem, probably because I have limited experience with regex.
Basically I have a long string of XML tags that is all on one line. I want to add line breaks at certain points to get the data more formatted and looking nice. I am using CodeMirror to display this data on a webpage but for some reason its all on line #1.
So I need to go from something like this:
<Sample><Name></Name><PhoneNumber><AreaCode></AreaCode><Number></Number></PhoneNumber></Sample>
To something like this:
<Sample>
<Name></Name>
<PhoneNumber>
<AreaCode></AreaCode>
<Number></Number>
</PhoneNumber>
</Sample>
CodeMirror will take care of the rest of the formatting all I need to do is insert the line breaks in the right spot using regex or a loop of some sort. The Tags will or can change so I am guessing regex has to be used.
I have had success inserting line breaks with \n and &#xD but can't seem to get regex to detect the proper locations.
Any help would be greatly appreciated. Thanks.
UPDATE
I overlooked this but the brackets are in fact being sent as < and >
So example tag would look like:
<PhoneNumber>
or
</PhoneNumber>
So basically need to insert a \n after every > that is a closing tag or a beginning tag that contains children tags.

There be dragons here.
I'd like to point you to a very similar question answered awhile ago that does a good job of explaining why you should NOT try to parse XML yourself unless you REALLY know what you're doing.
Use an XML deserializer if you want to get nice line breaks and that sort of thing.

Try this regex pattern:
>\s*<(?!/)
Replacement string : >\n<
UPDATE:
>\s*<(?!/)

Related

notepad++: create regular expression to change color of comment following comment

I know that every syntax highlight in notepad++ is based on regular expressions configured for the current language, but where do notepad++ stores the files relative to these regexes?
In my case, I want to highlight javascript comments that are placed following (or inside, using double slashes, if you prefer) comments in a different way of standard comments, like that:
product(); //just a comment about what is being made on the execution part of the line
//sum(); //comment formatted the same way as the executional part, which is also commented
That is because I want to distinguish these comments from commented execution code. The commented execution line is '//sum();' in this case, as '//comment...' is the comment that was commented and is following the first comment. Both are gray, but I want notepad++ to use another color to show everything that is after the second pair of slashes.
Anybody can also help me in building this regex?

Ignoring whitespace in code and pre tags

I am having some issues regarding whitespace in my HTML code. This is what is currently looks like:
<pre>
<code>
-(void)respring {
system("killall -9 SpringBoard");
}
</code>
</pre>
The indentation marks are to format my HTML a little better, make it more readable. When I save it and upload it to my server, I get
this. I have already tried using the white-space CSS property to try to fix the issue, but all it does it mess up my codes formatting. I also looked at the JavaScript solutions to this problem via Google but they don't seem to work. I am using highlight js for syntax highlighting if that helps.
Thanks.
The whole point of a pre tag is to preserve the spacing that's displayed, including anything at the beginning of a line. This should fix it.
<code>-(void)respring {
system("killall -9 SpringBoard");
}</code>
It may not look pretty in code, but on the page it'll look just fine.

JavaScript: how to set a value containing quotes inside a textbox

I have an MVC-project where I store data in a database and one of my views contains textboxes to edit this data.
Because of the specifics I can't create the textboxes directly via TextBoxFor(), EditorFor() etc. but have to affect the value with JavaScript, so what i do is write the needed value in the javascript code at page loading, and this code is later on triggered to affect the textbox value.
$("#textboxID").val("#HTML.Raw(Model.value)");
This workes fine until one of strings has got quotes in it.
When i input it directly like
$("#textboxID").val("#Model.value");
it will be HTML-encoded with the quotes written as > & quot; (without space of cource)
What i found out is that the only way to output quotes correctly is by escaping them with backslash \ however i can't seem to find a helper to do that.
Is there a solution? Am i doing anything wrong?
For now, i found a workaround inspired by Filipe Borges suggestion
#Html.Raw(Html.Encode(Model.Libelle).Replace(""", "\\\""))
It's very ugly, but at least it solves the problem, I appreciate anyone suggesting a better solution.
For now, i found a workaround inspired by Filipe Borges suggestion
#Html.Raw(Html.Encode(Model.Libelle).Replace(""", "\\\""))
It's very ugly, but at least it solves the problem, I appreciate anyone suggesting a better solution.
Solved using:
#Html.Raw(Html.Encode(Model.value).Replace(""", "\\\""))
First suggested replacing " by \" using #Model.value.replace(""", "\\\""), but it does not work because the value only contains " after the default html encoding is applied by mvc.
Edit: Final solution by Souhaieb Besbes. Edited mine to not keep it wrong.
You can use single quote to show double quotes as following:
$("#textboxID").val('"'+#Model.value+'"');

Grunt - Replace blank lines in a file - regex

I'm building up my first Grunt project and one of my tasks is introducing blank lines into the beginning of my output html files. I'd like to remove them.
I found grunt-line-remover but it requires me to list each html file separately, and I'd rather point it to a directory. (and I don't have time right now to figure out how to tweak the code to do that.)
I then found grunt-text-replace which supports file name pattern matching, but I can't figure out the Regex for finding a blank line in an html file.
I tried
/\n/g
but that replaces every new line whereas I'd like to just replace the empty lines. Really, I just want to replace the beginning blank lines, but if it removes every blank line that's fine.
Any help would be appreciated
If you want to remove empty space Try using a regex of [\r\n]{2,}
Instead of looking for newlines, you can look for empty lines with the regex
/^\s*$/gm
It should give you empty lines as well as lines containing spaces only.

Programmatically removing newlines inside Google Documents

I wrote a trivial google apps script that runs thru a google document to divide pieces of text. Now, the text that my code need to divide seems to include newlines.
My problem is that I just don't understand how to remove these programmatically (I tried: mydoc.replaceText("/\n/",""); and mydoc.replaceText(/(\r\n|\n|\r)/gm,""); plus other similar variations with no success.
I can't comment but maybe something like this works for you? replaces whitespace with single spaces
mydoc.replaceText("/\s/"," ");

Categories

Resources