remove line break that appears after a certain text from textarea - javascript

how do I remove line breaks or empty lines that appear after a certain text in a textarea box. I tried something like this:
$('#mytextarea').val($('#mytextarea').val().replace('some_Text' + "\n", ''));
But that didn't work

var newVal = $('#mytextarea').val().replace(/some_Text[\r\n]+/, 'some_Text')

I found this regex JavaScript line breaks
$('#mytextarea').val($('#mytextarea').val().replace(/(\r\n|\n|\r)/gm,""));

I'm just concentrating on the regexp part of the question:
Sample Text and Replacement Code
"some texts are better than others, some_text\nsome_text\r\nsome_text\rok? some_text"
.replace(/(some_text)[\r\n]+/g, '')
Result
"some texts are better than others, ok? some_text"

Related

Replace anything in certain position in string using javascript regular expression

I am trying to remove empty all paragraph tags, regardless of what style attributes might be in the p tag, from a string. I want to remove, for example, all of these and replace with an empty string.
<p style="margin-left:0px"></p>
<p></p>
<p style="margin-left:1cm; margin-right:1cm"></p>
So far, to deal with one situation I have, I am doing this:
str = str.replace(/<p style=\"margin:0cm 0cm 10pt\"><\/p>/g,'')
which is working in that particular situation. How can I write it so it removes
<p AnythingHereInThisTag></p>
and replaces it with an empty string?
Edit - further to answer below - if I do this:
str = str.replace(/<p(.*)><\/p>/g,'')
it is replacing the whole string which might look like
<p>Hello</p><p>Some text in the middle</p><p>Goodbye</p>
It needs to look at each pair of tags
Replace Any charecter without a char " has the regex as [^\"]
var reg=/\<p( [a-zA-Z]*=((\"[^\"]*\")|(\'[^\']*\')))*\>\<\/p\>/g;
console.log('<p style=\'margin:0cm 0cm 10pt\'></p>'.replace(reg,''));
console.log('<p style=\"margin:0cm 0cm 10pt\"></p>'.replace(reg,''));
console.log('<p style=\"margin:0cm 0cm 10pt\" class=\"test\"></p>'.replace(reg,''));
console.log('<p></p>'.replace(reg,''));
Something like this?
str = 'asd<p style="margin-left:0px"></p><p></p><p style="margin-left:1cm; margin-right:1cm"></p>'
str.replace(/<p(.*)><\/p>/g,'') // "asd"
Reading the question again, it is unclear if you wanted to remove only the attributes within the tag, or the tag completely. Please clarify.
You can read more about regular expression here.

Simulate paste into Textarea with javascript

When pasting data from the clipboard into an html textarea, it does some pretty cool parsing to make the paste look close to what was copied as far as newlines go.
For example, I have the following html on a page that I select (everything highlights in blue) and then I copy it:
Hello
<br>
<br>
<br>
<div>more</div>
<div>last</div>
So to be clear, what I am copying is what the output of this jsfiddle looks like.
Now when I paste this magical, copied text into a text area, I see exactly what I would expect: "Hello", empty line, empty line, empty line, "more", "last". Cool! Then when I use jQuery's .val() on the textarea, I get "Hello\n\n\nmore\nlast". Super cool. It took the br's and div's and was able to infer the correct newlines from it.
Now...
What I am trying to do it programmatically take the same data I copied earlier and set it as the textarea's value as if it were pasted.
Here is what I have tried...
So, say the stuff I copied earlier was wrapped in a <div id="parent">...</div>.
var parent = $("#parent");
var textarea = $("#theTextArea");
// Set the value of the text area to be the html of the thing I care about
textarea.val(parent.html());
Now I know this isn't the same as a copy-paste, but I was hoping it would take care of me and do what I wanted. It doesn't. The textarea gets filled with Hello<br><br><br><div>more</div><div>last</div>. The html that was once invisible is now stringified and made part of the text.
Obviously I did this wrong. .html() returns, of course, the string of html. But is there something I could call on parent that would give me the text with all inferred linebreaks?. I have tried calling parent.text(), but this only gives Hellomorelast (no line breaks).
A few notes that could help with an answer: I am using Angular, so I have access to all their goodies and jQuery.
Edit:
Solution
It is not nice but you can try to replace html tags with line breaks '\n' or do some line breaks in the html file and get the content with text().
var parent1 = $("#paren1");
var textarea1 = $("#theTextArea1");
var parent2 = $("#paren2");
var textarea2 = $("#theTextArea2");
// Set the value of the text area to be the html of the thing I care about
var text = parent1.html();
text = text.replace(new RegExp("<br>", 'g'),"\n");
text = text.replace(new RegExp("<div>", 'g'),"");
text = text.replace(new RegExp("</div>", 'g'),"\n");
textarea1.val(text);
textarea2.val(parent2.text());
JSFiddle

How to test content of html element with qunit

I have an html element that contains other elements (a, span) which contain text like "this is a text". As some words are inside a or span or both the result of jqueries text() is something like " this\n is a\n text ". I'm not interested in all those whitespace. I just want to test if "this is a text" is the readable text in the element.
I could workaround this by using regex, e.g. assert.ok(string.match(/^\s*this\s+is\s+a\s+text\s*^/)) but this is kinda ugly plus I'd loose the "expected: ... , result: ...." stuff in the test report. Is there a better way to test the text contents of html elements with qunit/jquery?
Update:
There seems to be no other way/convenience method in qunit to solve that, except for manually messing with the whitespaces like in https://stackoverflow.com/a/27779636/1250436
You can remove extra spaces by following code.
string = " this is a text ";
string = string.replace(/[\t\n\s]+/g, " "); //For removing space,tab and new line
alert(string);

HTML5 Href "sms" content with break line tag?

I want to use html link href to text message(sms) in android smart phone.But i try want the breakline in my message content.I have to display my message in messengers like below
"adgsd
sdgsf
asdfdf"
I try use "< br >","< br/ >","/n","/r" also cant break line.
Any Idea?Below is sample code with replace the < br > to other tag.
link = "smsto:" + "601" + "?body=" + document.getElementById("DetailContent").innerHTML.replace(/<br>/g, '\n');
document.getElementById("smsDetailLink1").setAttribute("href", link);
"%0D%0A"
with this format can get break line.
For anyone wondering, the answers given above are just the hex ASCII codes for the DOS/Windows line ending CR-LF (\r\n), i.e. carriage return and line feed. A lot of you probably knew that, some may not.

regex to replace a string from the inner html of a div

i have a div that contains some html and text (html is added dynamically)the structure would be like
<div id="contentContainer">
<span>ProductA</span> ; <span>ProductB</span>; prod
</div>
i want to remove the last incomplete text (prod) from the inner html of the div contentContainer on submit button click
for this i was using regex returnText.replace(/\w+$/, ''); and it works fine
as i can not trim the text to last index of ';'
but not the issue is when user puts some special charaters in the incomplete text as pr\od
the regex fails
so is there any solution to trim the last appended text the inner html of the div
or can i trim the text to the last html tag and place ; after that
please suggest any solution
if you are using jquery you can pull out all span elements and replace innerHTML with them.
$("#contentContainer").html( $("#contentContainer span") );
That should clean rest things. Maybe not the best but i think its better then regexp on content.
Solution looks at the last DOM node in DIV, if it is a text node it changes text to semi-colon
var lastNode = $('#contentContainer').contents().last()[0]
if (lastNode.nodeType == 3) {
lastNode.textContent=';'
}
demo: http://jsfiddle.net/EhcLh/

Categories