How remove a line of a string in Java Script - javascript

I would like to remove a specific line of a string using the replace.
If it is not possible with a replace, like some suggestion how i can delete a line, for example, my text have 3 lines, and i need remove line 2 or line 3... Why I can make this?
this is my first line and here this is my second line -> i need
delete this line here is another line
replace( "and here this is my second line", WHAT I NEED USE HERE TO REMOVE THIS LINE );
Solution is use splice first and join after remove line:
var str="this is my first line\nand here this is my second line -> i need delete this line\nhere is another line";
ar=str.split('\n'); ar.splice(1,1); str=ar.join('\n'); // This remove line 2
ar=str.split('\n'); ar.splice(2,1); str=ar.join('\n'); // This remove line 3
console.log(str);

Given that
var text = "";
text += "this is my first line\n";
text += "and here this is my second line\n";
text += "here is another line\n";
If you need to remove the first occurrence of the string use this:
text = text.replace("and here this is my second line\n", '');
And if you need to remove all the occurrences of the string use this one:
text = text.replace(/and here this is my second line\n/g, '');
Take into account that in the second example regular expression are used.
\n is the special character for line breaks and might be in different positions in your specific code

You can simply remove the line by replacing the target line with an empty string.
$(.someDivClass).text().replace("text to replace", "");
However I don't think it would completely remove the element containing that text, if that's what you're looking for.
With standard JS, you use this snippet in a method and make some action trigger that event such as a button click:
if(document.getElementById("id of <p> tag").innerHTML == "specific text")
document.getElementById("id of <p> tag").innerHTML = '';
I am not sure what your intentions are with removing the text and whether or not you want to completely remove the element containing the text, so this is the best solution I can offer. If you explain what your intentions are and how it will be applied to your project, I can provide a better solution.

Related

finding length of a sentence in a cell, in google apps script

I'm pretty new to this scripting and all, I want to find the length of a sentence in a cell in google sheet. Example, "I want to check the length" is a sentence in cell A1 in google sheet, I want to find the length of this string.
And one more, if I want to replace text like space(" ") with any thing like plus (+), I have used script as shown below
var newdata = olddata.toString().replace(" ","+");
// here old data refers to the string "I want to check length"
// output that displayed in log is like "I+want to check length"
// Output, that I want i like "I+want+to+check+length"
when I used this, in log I can see only, first space is getting replaced in the whole string/sentence. so how can modify/alter my code in order to replace all the spaces with any desired character.
The length of the string is given by newString.length property.
let oldString = "I want to check the length";
console.log(oldString.length);
The replace() function uses regular expressions to replace the strings. To replaces all matched patters, you will need to use /g global match. To match all spaces, you need to provide /\ /g to the replace() function. See example:
let oldString = "I want to check the length";
let newString = oldString.replace(/\ /g, "+");
console.log(newString);
console.log(newString.length);

removing specific html tags in a string - javascript

I'm trying to clean up a string of text on the server side from the output generated by a wysiwyg. and while I can fix it client side, it's best to also fix this on the server side.
var string = "<p>firstline</p><p>secondline</p><p>thirdline</p><p>iframe</p><p>a</p><p>df</p><p>dsf </p><p><br></p><p>sd</p><p>f</p><p>sdf</p><p><br></p>"
var x = string.replace("<p><br></p>", "");
https://jsfiddle.net/8c0yh9r7/
the code should but doesn't get rid of the break within the paragraphs
why is that?
Use a regex with a global flag, like:
string.replace(/<p><br><\/p>/g, "");
https://jsfiddle.net/Lu2r3820/1/
When using a string only the first occurrence will be replaced.
See replace() documentation
doesn't get rid of the break within the paragraphs
Yes, it does… but only once. You have more than one paragraph containing a line break in your code.
If you want to replace it more than once, you need to use a regex and mark it as global with g.
var x = string.replace(/<p><br><\/p>/g, "");
It does replace, but only the first occurrence. If you run this afterwards, you can see the second occurrence disappearing.
var x = x.replace("<p><br></p>", "");
refer to this to replace all occurrences.
How to replace all occurrences of a string in JavaScript?

Javascript Regex only replacing first match occurence

I am using regular expressions to do some basic converting of wiki markup code into copy-pastable plain text, and I'm using javascript to do the work.
However, javascript's regex engine behaves much differently to the ones I've used previously as well as the regex in Notepad++ that I use on a daily basis.
For example- given a test string:
==Section Header==
===Subsection 1===
# Content begins here.
## Content continues here.
I want to end up with:
Section Header
Subsection 1
# Content begins here.
## Content continues here.
Simply remove all equals signs.
I began with the regex setup of:
var reg_titles = /(^)(=+)(.+)(=+)/
This regex searches for lines that begin with one or more equals with another set of one or more equals. Rubular shows that it matches my lines accurately and does not catch equals signs in the middle of contet. http://www.rubular.com/r/46PrkPx8OB
The code to replace the string based on regex
var lines = $('.tb_in').val().split('\n'); //use jquery to grab text in a textarea, and split into an array of lines based on the \n
for(var i = 0;i < lines.length;i++){
line_temp = lines[i].replace(reg_titles, "");
lines[i] = line_temp; //replace line with temp
}
$('.tb_out').val(lines.join("\n")); //rejoin and print result
My result is unfortunately:
Section Header==
Subsection 1===
# Content begins here.
## Content continues here.
I cannot figure out why the regex replace function, when it finds multiple matches, seems to only replace the first instance it finds, not all instances.
Even when my regex is updated to:
var reg_titles = /(={2,})/
"Find any two or more equals", the output is still identical. It makes a single replacement and ignores all other matches.
No one regex expression executor behaves this way for me. Running the same replace multiple times has no effect.
Any advice on how to get my string replace function to replace ALL instances of the matched regex instead of just the first one?
^=+|=+$
You can use this.Do not forget to add g and m flags.Replace by ``.See demo.
http://regex101.com/r/nA6hN9/28
Add the g modifier to do a global search:
var reg_titles = /^(=+)(.+?)(=+)/g
Your regex is needlessly complex, and yet doesn't actually accomplish what you set out to do. :) You might try something like this instead:
var reg_titles = /^=+(.+?)=+$/;
lines = $('.tb_in').val().split('\n');
lines.forEach(function(v, i, a) {
a[i] = v.replace(reg_titles, '$1');
})
$('.tb_out').val(lines.join("\n"));

Replacing an exact text from textarea

I have this little problem with jQuery. I want to remove an specific text from textarea. Check my codes:
Textarea values:
aa
a
aaa
i tried this:
$("#id_list").val($("#id_list").val().replace("a", " "));
The codes above only works if the text in each line is unique with no matching characters from other lines.
Now the problem is the codes above removes the first letter from aa, instead of removing the second line a. How can I get it to work in replace/removing an exact word on a line from textarea? Any help would be much appreciated.
Use word boundary.
Do this:
$("#id_list").val($("#id_list").val().replace(/\ba\b/g, " "));
That will replace only a
If you want to replace just one time, remove g from my regex.
If you want to use strings stored in a variable, do this:
var word = "a";
var regex = new RegExp("\\b"+word+"\\b","g");
$("#id_list").val($("#id_list").val().replace(regex, " "));
Just use replace(/a/g, " ")) instead. the /g flag means you search globally for the "a" letter. Without it you just replace the first occurrence.
You need to use regex replace
replace(/a/g, " "))

Javascript: Removing first text line if string starts with certain characters

I wrote a script for some text manipulation, and want to add a function so that the first line of my input string is removed if it starts with the symbol '>'. I tried to find a solution for hours now, but I cannot get it to work: Somehow the first line is not removed properly.
My input text should be processed as it is if it looks like this:
"HABHHIKKLLMMNMNIL"
and the first line should be removed if it looks like this:
"
>some text
HABHHIKKLLMMNMNIL
"
My current solution would look like this:
// remove first line if starts with '>'
if (sequence_str.substring(0) === '>'){
// break the textblock into an array of lines
var lines = sequence_str.split('\n');
// remove one line, starting at the first position
lines.splice(0,1);
// join the array back into a single string
var sequence_str = lines.join('\n');
}
sequence_str = sequence_str.replace(/(\r\n|\n|\r)/gm,""); //remove linebreaks
I would appreciate any ideas, and I hope some could help me to figure out where the problem is.
Thanks!
Why not just this?
sequence_str = sequence_str.replace(/^>.*/,'');
This will remove the first line if it starts with >. It will leave an empty line (the newline character is unaffected), but since you're later stripping all line-breaks, it doesn't matter.
just try this
"lines = lines.splice(0,1);"
instead of "lines.splice(0,1);"
Thank you so much, I really like the short codes you suggested.
The error was that I used
if (sequence_str.substring(0) === '>')
where it should be
if (sequence_str.substring(0,1) === '>')
I am a JavaScript beginner and come from the Python corner, please excuse my stupid mistake
EDIT:
If someone is interested to see how the end product looks like:
http://bluewoodtree.zxq.net/crop_and_cleave.html

Categories

Resources