replace all occurance of a character in jQuery or javascript - 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\""

Related

remove all empty values from url string

I'm trying to remove all empty params from a url string. My url looks like this
http://localhost/wm/frontend/www/?test=&lol=1&boo=2
my code should return
http://localhost/wm/frontend/www/?lol=1&boo=2
but it doesn't instead it returns
http://localhost/wm/frontend/www/?&lol=1&boo=2
This is the regex i'm using replace("/(&?\w+=((?=$)|(?=&)))/g","") i know i could just use replace() strings that match '?&' after the 1st replace, but i would rather edit my regex to do so, so it's in 1 line of code. Any ideas?
here is my jsfiddle
You can use this regex for replacement:
/[^?&=]+=(?:&|$)|&[^?&=]+=(?=&|$)/g
And replace it by:
""
RegEx Demo
Try
/\w+=&|&\w+=$/g,
var url = "http://localhost/wm/frontend/www/?test=&lol=1&boo=2&j=";
document.write(url.replace(/\w+=&|&\w+=$/g, ""))
Just try to invoke native's 'replace', which could be used with a regex in its first argument.
str.replace(regex, replace_str)
Please, see this fiddle to see a running example: http://jsfiddle.net/xvqasgmu/1/
You can for example say:
var s = 'http://localhost/wm/frontend/www/?test=&lol=1&boo=2&r=';
s = s.replace(/\w*=\&/g, '');
s = s.replace(/&\w*=$/g, '');
That is, remove a block of letters + = + &. Then, remove & + letters + = at the end of the line (indicated by $).
For your input, it returns:
http://localhost/wm/frontend/www/?lol=1&boo=2
See it in JSFiddle or directly here:
var s = 'http://localhost/wm/frontend/www/?test=&lol=1&boo=2&r=';
s = s.replace(/\w*=\&/g, '');
s = s.replace(/&\w*=$/g, '');
document.write(s)
Test
If the input contains blocks in the middle and in the end:
http://localhost/wm/frontend/www/?test=&lol=1&boo=2&r=
the code I wrote above returns:
http://localhost/wm/frontend/www/?lol=1&boo=2

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

regex replace in javascript/jquery

I've never really used regex so this is probably a basic question, but I need to reformat a string in javascript/jquery and I think regex is the direction to go.
How can I convert this string:
\"1\",\"2\",\"\\",\"\4\"
into:
"1","2","","4"
These are both strings, so really they'd be contained in "" but I thought that may confuse things even more.
I've tried the following but it doesn't work:
var value = '\"1\",\"2\",\"\\",\"\4\"'.replace(/\"/, '"').replace(/"\//, '"');
Try:
var value = your_string.replace(/\\/g, "");
to remove all the "\"
It's a lot of escaping... Your string is:
var str = '\\"1\\",\\"2\\",\\"\\\\",\\"\\4\\"'
console.log(str.replace(/\\/g, '')) // "1","2","","4"
However, if you want only to replace \" with " use:
console.log(str.replace(/\\"/g, '"')) // "1","2","\","\4"

to replace " " with

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.

Categories

Resources