to replace " " with - javascript

this is my javascript code:
mystring = "this is sample";
nestring = mystring.replace(/ /g," ");
I need nestring ouput "this is sample".
but with the code above, nestring = "this is sample"
how can I replace space( ) with &nbsp( )?
Many thanks!
re:
I embed svg in html. But ie, chrome do not support xml:space=preserve, firefox does. By replace " " with &nbsp, multiple " " will not condense to one " ".

You could use the Unicode:
nestring = mystring.replace(/ /g, "\u00a0");
But your example did exactly what you told it to.

If you want to replace the characters with character code 32 (0x20) with character with character code 160 (0xA0), you can use the fromCharCode method to create the string to replace with:
nestring = mystring.replace(/ /g, String.fromCharCode(160));

Your code works fine, but if you are writing the result to the document you will only see the spaces because they are HTML encoded.
If you alert(nestring); you will see that the spaces are replaced.

Related

replace all occurance of a character in jQuery or javascript

I want to replace " with \"
I am using
content.replace(/"/g,'\\"')
Now I want to do this only when " occurs without \ before it
For example
I want "title" to be replaced as \"title\"
But if the string is already \"title\" I don't want to replace " to \"
Try content.replace(/["]/g, "\\$&").
Example:
var content = '"title", \"title", \"title\"';
content.replace(/["]/g, "\\$&");
Output: '\"title\", \"title\", \"title\"'
Hope it helps
content.replace(/(^|[^\\])"/g, '$1\\"');
Ok I think this works for me
var content = 'hello"world\"'
content.replace(/\\"/g,'"').replace(/"/g,'\\"')
This logs
"hello\"world\""

Javascript Replace doesn't work because of Escape Sequence

I need to replace every '\n' in a var with a <br> tag but the code I tried doesn't work. Here's my example and code:
Example:
Hello there \n Are you online? \n What do you mean?
Should become:
Hello there <br> Are you online? <br> What do you mean?
Code:
var text = 'Hello there \n Are you online? \n What do you mean?';
var find = '\n';
var re = new RegExp(find, 'g');
re = text .replace(re, '<br>');
What am I doing wrong?
var text = 'Hello there \n Are you online? \n What do you mean?';
var find = '\n';
var re = new RegExp(find, 'g');
re = text.replace(re, '<br>');
document.write(re);
Works fine
Working Fiddle
In your fiddle (which you did not post in your original question) there are several issues addressed already in the comments. But the crux of your problem -- that would have helped us help you faster -- is that you're pulling a string with "\n" from an innerHTML property. Those are not newline characters, because if you're seeing a literal "\n" instead of a newline, it means it has been escaped. Therefore, you need to use "\\n" in your regular expression, not "\n". Use the devtool consoles in modern browsers to inspect your variables and catch errors.
Code:
var el = document.getElementById("ado")
var text = document.getElementById("ado").innerHTML;
text = text.replace(/\\n/g, '<br>');
el.innerHTML = text
in your fiddle you wrote :
document.findElementById
instead of
document.getElementById
I think it is one of things that should solve your issue !

how to write a regex to match a "\n" which is not followed another "\n"?

I want to replace "\n" with ""(empty string) only when "\n" is not followed by another "\n".
var text = "abcdefg
abcdefg
1234556
1234556
ABCDEFG
ABCDEFG";
So, if I have a string as the above, I want to make it like this after replacing "\n"s.
"abcdefgabcdefg
12345561234556
ABCDEFGABCDEFG";
But, I can't find out how to write a regex to match a "\n" that is not followed another "\n".
These are what I tried, but these match both "\n" and "\n\n".
var pattern1 = new RegExp("\\n{1}");
var pattern2 = new RegExp("\\n(?!\\n)");
Could anyone please help me to write a regex in this case?
Thanks!
You can match the following:
[^\\n](\\n)[^\\n]
Demo: http://regex101.com/r/eP9xD1
You can use /([^\n])\n([^\n])/g and replace it with \1\2.
Usage:
> str = 'abcdefg\nabcdefg\n\n1234556\n1234556\n\nABCDEFG\nABCDEFG';
> str.replace(/([^\n])\n([^\n])/g, '\1\2');
"abcdefbcdefg
123455234556
ABCDEFBCDEFG"
REGEX DEMO
FIDDLE
You can use negative lookahead to match \n which aren't followed by another \n:
\n(?!\n)

Replacing " with \" in ASP

As the title suggests, I am trying to replacing a " with the escape character \". The reason I am doing this is because there is a quote in my string that represents inches and this string needs to be run through javascript code.
However, when I run my string in the javascript code, it only reads up to the inches character and forgets the rest of the string.
Any and all help is greaty appreciated.
A function like this will escape \, " and ':
function AddSlashes(str)
AddSlashes = replace(str,"\","\\")
AddSlashes = replace(AddSlashes,"'","\'")
AddSlashes = replace(AddSlashes,chr(34),"\" & chr(34))
end function
Source
If you define like this var s = 'some string';, the quote is work ok, but if you do like var s = "some string";, you should replace the quote to \", so the code is var s = "some string \ " with the quote";
also you need to replace the Vbcrlf and vbcr and vblf before the JS code

line breaks using javascript strings

I m breaking a line in javascript using \n for example: -
text = "this is a test" + "\n" + "another test";
and to show that in html i m using the code
text = text.replace('\n' , '<br>');
but the text i m getting is without the br
when checking in firebug console i get is with line break(\n not shown but newline created)
how can i place line breaks using \n or i have to do some custom code instead of \n
thanks
var newText = text.replace(/\n/g , "<br>");
use double quotes to not escape the \n
try this:
text = text.replace("\n" , "<br>");
/\r\n|[\r\n]/g <- this is what I use. I am a pessimist..

Categories

Resources