Reputation: 111
need to clear a textarea content
any combination of br
, \n
or spaces
before and after * * *
should be raplaced by \n\n
pls help
var story = $('#story');
$('button').on('click', function(){
let a = story.val();
a = a.replaceAll("<\p> + zero or multiple br, \n or spaces + * * * + zero or multiple br, \n or spaces + <p>", "<\p>\n\n* * *\n\n<p>");
story.val(a);
});
#story{
display:block;
width:100%;
height:99px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id='story'>
<p>lorem</p>
//here can be zero or multiple br, \n or spaces. should be replaced by \n\n
* * *
//here can be zero or multiple br, \n or spaces. should be replaced by \n\n
<p>ipsum</p>
</textarea>
<button>CLICK</button>
Upvotes: 1
Views: 46
Reputation: 28236
You could do something like this:
var story = $('#story');
$('button').on('click', function(){
let a = story.val();
a = a.replace(/(?:\s|<br>)*\* \* \*(?:\s|<br>)*/i,"\n\n* * *\n\n");
// a = a.replace(/<\/p>.*\* \* \*.*<p/si,"</p>\n\n* * *\n\n<p");
story.val(a);
});
#story{
display:block;
width:100%;
height:99px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id='story'>
<p>lorem</p>
//here can be zero or multiple br, \n or spaces. should be replaced by
<br>
* * *
//here can be zero or multiple br, \n or spaces. should be replaced by \n\n
<p>ipsum</p>
</textarea>
<button>CLICK</button>
There is some uncertainty with regards to "wild text", i. e. text-nodes that are not within a paragraph. These texts will remain where they are at the moment. If you also want them to dissappear you will need to do something like this:
a = a.replace(/<\/p>.**\* \* \*.*<p/si,"</p>\n\n* * *\n\n<p");
Upvotes: 2