adding newline in string concatenation - javascript

I have a textarea and I do console.log($("textarea").val()), the result can be
abc
123
And I have an input, i want to concate the value with the value of textarea, like this
input_value
abc
123
I tried <br> but the br will appear which is not I want. How to make newline in concatenation? use \n?

Use \n instead of <br>. Like that:
console.log($("input").val() + '\n' + $("textarea").val());

You can try something like this:
alert('abe \n' + 123);
\n Seems to do the trick for you.
Further Reading:
http://www.w3schools.com/jsref/jsref_regexp_newline.asp

Related

Javascript substring replace and output

Hi guys I wonder how to reconstruct string. I need to replace br tag inside string with '\n' new line character. So I'm simply doing it like this:
let a='Some<br>Text'
let b=a.replace(/<br>/gi, '\n');
But when I try to make an output to console this way:
console.log(JSON.stringify(b))
It shows the string like this:
Some\nText
But if I'm doing output this way:
console.log(b)
It returns:
Some
Text
So why? And is it possible to use console.log(JSON.stringify(b)) to show the string in a proper way. I mean like this:
Some
Text
Because the stringify method converts all the characters to string, so you wont see line breaks as expected. If you want to display your text on the same line, you can just replace (inside your regexp replacer) the newline '\n' with an empty space ' '
just replace br with an empty space instead of \n, since \n means a new line.This is why your second word starts from new line
let a='Some<br>Text'
let b=a.replace(/<br>/gi, " ");
output
Some Text

How to remove ' in java script

I have problem in removing ' with (blank and no space). Like Kello's to kellos.
I already tried this-
str = str.replace(/[\']/g, '');
But its not working.
Please help.
It actually does work:
var str = 'aa\'bb\'cc';
alert(str.replace(/'/g,'')); // aabbcc
alert(str.replace(/[\']/g,'')); // aabbcc
You do not need a character class, you just have to mask it if you use single quotes in JavaScript.
Also, keep in mind that ' (U+0027) is different from ’ (U+2019) and must be handled separately.
A Kello's
str.replace(/[{\ },{\'}]/g,"");
result AKellos

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, " "))

Regular expression for removing whitespaces

I have some text which looks like this -
" tushar is a good boy "
Using javascript I want to remove all the extra white spaces in a string.
The resultant string should have no multiple white spaces instead have only one. Moreover the starting and the end should not have any white spaces at all. So my final output should look like this -
"tushar is a good boy"
I am using the following code at the moment-
str.replace(/(\s\s\s*)/g, ' ')
This obviously fails because it doesn't take care of the white spaces in the beginning and end of the string.
This can be done in a single String#replace call:
var repl = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
// gives: "tushar is a good boy"
This works nicely:
function normalizeWS(s) {
s = s.match(/\S+/g);
return s ? s.join(' ') : '';
}
trims leading whitespace
trims trailing whitespace
normalizes tabs, newlines, and multiple spaces to a single regular space
Try this:
str.replace(/\s+/g, ' ').trim()
If you don't have trim add this.
Trim string in JavaScript?
Since everyone is complaining about .trim(), you can use the following:
str.replace(/\s+/g,' ' ).replace(/^\s/,'').replace(/\s$/,'');
JSFiddle
This regex may be useful to remove the whitespaces
/^\s+|\s+$/g
Try:
str.replace(/^\s+|\s+$/, '')
.replace(/\s+/, ' ');
try
var str = " tushar is a good boy ";
str = str.replace(/^\s+|\s+$/g,'').replace(/(\s\s\s*)/g, ' ');
first replace is delete leading and trailing spaces of a string.

How to replace all the \ from a string with space in javascript?

For example:
var str="abc\'defgh\'123";
I want to remove all the \ using Javascript. I have tried with several functions but still can't replace all the forward slashes.
I've posted a huuuge load of bollocks on JS and multiple replace functionality here. But in your case any of the following ways will do nicely:
str = str.replace('\\',' ');//Only replaces first occurrence
str = str.replace(/\\/g,' ');
str = str.split('\\').join(' ');
As #Guillaume Poussel pointed out, the first approach only replaces one occurrence of the backslash. Don't use that one, either use the regex, or (if your string is quite long) use the split().join() approach.
Just use the replace function like this:
str = str.replace('\\', ' ');
Careful, you need to escape \ with another \. The function returns the modified string, it doesn't modify the string on which it is called, so you need to catch the return value like in my example! So just doing:
str.replace('\\', ' ');
And then using str, will work with the original string, without the replacements.
str="abc\\'asdf\\asdf"
str=str.replace(/\\/g,' ')
You want to replace all '\' in your case, however, the function replace will only do replacing once if you use '\' directly. You have to write the pattern as a regular expression.
See http://www.w3schools.com/jsref/jsref_replace.asp.
Try:
string.replace(searchvalue,newvalue)
In your case:
str.replace('\\', ' ');
Using string.replace:
var result = str.replace('\\', ' ');
Result:
"abc 'defgh '123"

Categories

Resources