Suppose that I have a text field in which a user can submit code snippets. I want to detect when a specific word occurs in the string and then do something with the words/characters that come after that word.
Let's say we have a string and that after the word pyjamas I want to start the rest of the code on a new line without an indent. (Very similar to how code beautifiers work.) The output will be rendered inside pre
, so I don't want any <br>
tags or other HTML tags.
There are some catches though.
- Everything following a word (pyjamas) has to start on a new line on the same "level" (equally amount of tab indents) as the line before.
- Commas should always start on a new line and reverse indented with a tab
- When there is another character, let's say an exclamation mark
!
, the code following has to start on a new line with a tab as an indent.
Example:
Input:
Bananas! Apples and pears walk down pyjamas the street! and they say pyjamas hi to eachother, pyjamas But then! some one else comes pyjamas along pyjamas Who is he?, pyjamas I don't know who! he is pyjamas whatever,,
Output:
Bananas!
Apples and pears walk down pyjamas
the street!
and they say pyjamas
hi to eachother
, pyjamas
But then!
some one else comes pyjamas
along pyjamas
Who is he?
, pyjamas
I don't know who!
he is pyjamas
whatever
,
,
I am working with jQuery, so you can use it if you want.
Here is a fiddle with the code above, so you can test it out. My result thus far is not great at all. (Type something in the textarea, the output will change.) As I'm currently only barely knowledgeable with regex, I am in need of some help.
What I have so far:
var a = $("textarea").val(),
b = a.split('!').join("!\n "),
c = b.split('pyjamas').join("pyjamas \n");
$("textarea").keyup(function() {
$("#output>pre").html(c);
});