This question already has answers here:
How to escape apostrophe or quotes on a JSP (used by JavaScript)
(9 answers)
Closed 9 years ago.
JSP:
<% final String data = "some test with ' single quotes"; %>
<script>
var str = '<%= data %>';
<script>
The result is (JavaScript):
var str = 'some test with ' single quotes';
Uncaught SyntaxError: Unexpected identifier
How do I replace this single quote with \' to avoid a JavaScript error?
Use escapeEcmaScript method from Apache Commons Lang package:
Escapes any values it finds into their EcmaScript String form. Deals
correctly with quotes and control-chars (tab, backslash, cr, ff, etc.).
So a tab becomes the characters '\\' and 't'.
The only difference between Java strings and EcmaScript strings is
that in EcmaScript, a single quote and forward-slash (/) are escaped.
Example:
input string: He didn't say, "Stop!"
output string: He didn\'t say, \"Stop!\"
Remember you also need to encode the double quotes, new lines, tabs and many other things. One way to do it is using org.apache.commons.lang.StringEscapeUtils
public class JavaScriptEscapeTest {
public static void main(String[] args) throws Exception {
String str = FileUtils.readFileToString(new File("input.txt"));
String results = StringEscapeUtils.escapeEcmaScript(str);
System.out.println(results);
}
}
input.txt
Here is some "Text" that
I'd like to be "escaped" for JavaScript.
I'll try a couple special characters here: \ "
output
Here is some \"Text\" that\r\nI\'d like to be \"escaped\" for JavaScript.\r\nI\'ll try a couple special characters here: \ \"
Are you looking to simply double-escape your string so you actually have a \ followed by a '?
<% final String data = "some name with \\' single quote"; %>
From your JavaScript code, you can use the string replace functionality:
var str2 = str1.replace("'","\\'");
Related
This question already has answers here:
What is the backslash character (\\)?
(6 answers)
Closed last year.
I am not able to get the message value in the desired format.
String url = "sample"
String message ="/test{\"url\":"' + url + '\"}
The desired value of message is "/test{\"url\":\"sample\"}"
Any idea on this?
Try with this:
String url = "sample";
String message ="/test{\\\"url\\\":\""+url+"\\\"}";
Or you can use String.format:
String url = "sample";
String message = String.format("/test{\\\"url\\\":\"%s\\\"}",url);
Try the following syntax:
String url = "sample";
String message ="/test{\\\"url\\\":\"" + url + "\\\"}";
Please note that back-slash \ and double-quote " are specialized character and hence they need to be escaped using back-slash \.
Hence, \\ is used for \ and \" is used for " in String literal.
Output:
/test{\"url\":"sample\"}
After several tried, I found the solution:
StringBuilder sb=new StringBuilder();
sb.append("\"/test{");
sb.append("\"\\");
sb.append("\"");
sb.append("url\\\"");
sb.append(":");
sb.append("\\\"");
sb.append(url);
sb.append("\"\\}\"");
System.out.println(sb.toString());
I am using java replaceAll() method to replace part of String with another String and its working great but, the problem comes when my file name contains characters like $ ^ + ( ) { } [ ] etc. In this case pattern matching fails and the original String remains as it is.
Sample code to show case my use case is as follow:
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file+name.jpeg", "cid: 14890411127853");
System.out.println(messageBody);
The expected output is:
src="cid: 14890411127853" style="height:225px"
but it gives:
src="http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg" style="height:225px"
How can I get it working by ignoring special characters that we use to form regex expression from my file name.
Thanks in advance!
You have unescaped metacharacters in your URL pattern, including a plus and a literal dot. Escape them, using the following pattern:
(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\+name\\.jpeg
^^^ escape dot and plus sign
Full code:
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\+name\\.jpeg", "cid: 14890411127853");
System.out.println(messageBody);
Output:
src="cid: 14890411127853" style="height:225px"
Update:
If you don't know in advance what the exact pattern will be, but you know it might have metacharacters, which would require escaping for use in a replacement, then Java provides a method for this: Pattern.quote()
To see how it works, we can split your pattern into two parts:
String part1 = "(http|https)://(?:[^\\s]*)";
String part2 = Pattern.quote("/FileDownloader/4/outbound/31358/file+name.jpeg");
messageBody = messageBody.replaceAll(part1 + part2, "cid: 14890411127853");
From the documentation for Pattern.quote():
This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.
Metacharacters or escape sequences in the input sequence will be given no special meaning.
You just have to escape those characters using a backslash (\)
example:
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\+name\\.jpeg", "cid: 14890411127853");
similarly
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file$name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\$name\\.jpeg", "cid: 14890411127853");
Did it this way.
final String[] metaCharacters = {"^","$","{","}","[","]","(",")",".","+","-","&"};
String filePath = "/4/outbound/31358/file+name.jpeg";
for(String c: metaCharacters){
if(filePath.contains(c)){
filePath = filePath.replace(c, "\\"+c);
}
}
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
System.out.println(messageBody);
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader"+filePath, "cid: 14890411127853");
System.out.println(messageBody);
I have an app that received a malformed JSON string like this:
{'username' : 'xirby'}
I need to replaced the single quotes ' with double quoates "
With these rule (I think):
A single quote comes after a { with one or more spaces
Comes before one or more spaces and :
Comes after a : with one more spaces
Comes before one or more spaces and }
So this String {'username' : 'xirby'} or
{ 'username' : 'xirby' }
Would be transformed to:
{"username" : "xirby"}
Update:
Also a possible malformed JSON String:
{ 'message' : 'there's not much to say' }
In this example the single quote inside the message value should not be replaced.
Try this regex:
\s*\'\s*
and a call to Replace with " will do the job. Look at here.
Instead of doing this, you're better off using a JSON parser which can read such malformed JSON and "normalize" it for you. Jackson can do that:
final ObjectReader reader = new ObjectMapper()
.configure(Feature.ALLOW_SINGLE_QUOTES, true)
.reader();
final JsonNode node = reader.readTree(yourMalformedJson);
// node.toString() does the right thing
This regex will capture all appropriate single quotes and associated white spaces while ignoring single quotes inside a message. One can replace the captured characters with double quotes, while preserving the JSON format. It also generalizes to JSON strings with multiple messages (delimited by commas ,).
((?<={)\s*\'|(?<=,)\s*\'|\'\s*(?=:)|(?<=:)\s*\'|\'\s*(?=,)|\'\s*(?=}))
I know you tagged your question for java, but I'm more familiar with python. Here's an example of how you can replace the single quotes with double quotes in python:
import re
regex = re.compile('((?<={)\s*\'|(?<=,)\s*\'|\'\s*(?=:)|(?<=:)\s*\'|\'\s*(?=,)|\'\s*(?=}))')
s = "{ 'first_name' : 'Shaquille' , 'lastname' : 'O'Neal' }"
regex.sub('"', s)
> '{"first_name":"Shaquille","lastname":"O\'Neal"}'
This method looks for single quotes next to the symbols {},: using look-ahead and look-behind operations.
String test = "{'username' : 'xirby'}";
String replaced = test.replaceAll("'", "\"");
Concerning your question's tag is JAVA, I answered in JAVA.
At first import the libraries:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Then:
Pattern p = Pattern.compile("((?<=(\\{|\\[|\\,|:))\\s*')|('\\s*(?=(\\}|(\\])|(\\,|:))))");
String s = "{ 'firstName' : 'Malus' , 'lastName' : ' Ms'Malus' , marks:[ ' A+ ', 'B+']}";
String replace = "\"";
String o;
Matcher m = p.matcher(s);
o = m.replaceAll(replace);
System.out.println(o);
Output:
{"firstName":"Malus","lastName":" Ms'Malus", marks:[" A+ ","B+"]}
If you're looking to exactly satisfy all of those conditions, try this:
'{(\s)?\'(.*)\'(\s)?:(\s)?\'(.*)\'(\s)?}'
as you regex. It uses (\s)? to match one or zero whitespace characters.
I recommend you to use a JSON parser instead of REGEX.
String strJson = "{ 'username' : 'xirby' }";
strJson = new JSONObject(strJson).toString();
System.out.println(strJson);
I want split a string like this:
C:\Program\files\images\flower.jpg
but, using the following code:
String[] tokens = s.split("\\");
String image= tokens[4];
I obtain this error:
11-07 12:47:35.960: E/AndroidRuntime(6921): java.util.regex.PatternSyntaxException: Syntax error U_REGEX_BAD_ESCAPE_SEQUENCE near index 1:
try
String s="C:\\Program\\files\\images\\flower.jpg"
String[] tokens = s.split("\\\\");
In java(regex world) \ is a meta character. you should append with an extra \ or enclose it with \Q\E if you want to treat a meta character as a normal character.
below are some of the metacharacters
<([{\^-=$!|]})?*+.>
to treat any of the above listed characters as normal characters you either have to escape them with '\' or enclose them around \Q\E
like:
\\\\ or \\Q\\\\E
You need to split with \\\\, because the original string should have \\. Try it yourself with the following test case:
#Test
public void split(){
String s = "C:\\Program\\files\\images\\flower.jpg";
String[] tokens = s.split("\\\\");
String image= tokens[4];
assertEquals("flower.jpg",image);
}
There is 2 levels of interpreting the string, first the language parser makes it "\", and that's what the regex engine sees and it's invalid because it's an escape sequence without the character to escape.
So you need to use s.split("\\\\"), so that the regex engine sees \\, which in turn means a literal \.
If you are defining that string in a string literal, you must escape the backslashes there as well:
String s = "C:\\Program\\files\\images\\flower.jpg";
String[] tokens=s.split("\\\\");
Try this:
String s = "C:/Program/files/images/flower.jpg";
String[] tokens = s.split("/");
enter code hereString image= tokens[4];
Your original input text should be
C:\\Program\\files\\images\\flower.jpg
instead of
C:\Program\files\images\flower.jpg
This works,
public static void main(String[] args) {
String str = "C:\\Program\\files\\images\\flower.jpg";
str = str.replace("\\".toCharArray()[0], "/".toCharArray()[0]);
System.out.println(str);
String[] tokens = str.split("/");
System.out.println(tokens[4]);
}
I have a bunch of HTML files. In these files I need to correct the src attribute of the IMG tags.
The IMG tags look typically like this:
<img alt="" src="./Suitbert_files/233px-Suitbertus.jpg" class="thumbimage" height="243" width="233" />`
where the attributes are NOT in any specific order.
I need to remove the dot and the forward slash at the beginning of the src attribute of the IMG tags so they look like this:
<img alt="" src="Suitbert%20%E2%80%93%20Wikipedia_files/233px-Suitbertus.jpg" class="thumbimage" height="243" width="233" />
I have the following class so far:
import java.util.regex.*;
public class Replacer {
// this PATTERN should find all img tags with 0 or more attributes before the src-attribute
private static final String PATTERN = "<img\\.*\\ssrc=\"\\./";
private static final String REPLACEMENT = "<img\\.*\\ssrc=\"";
private static final Pattern COMPILED_PATTERN = Pattern.compile(PATTERN, Pattern.CASE_INSENSITIVE);
public static void findMatches(String html){
Matcher matcher = COMPILED_PATTERN.matcher(html);
// Check all occurance
System.out.println("------------------------");
System.out.println("Following Matches found:");
while (matcher.find()) {
System.out.print("Start index: " + matcher.start());
System.out.print(" End index: " + matcher.end() + " ");
System.out.println(matcher.group());
}
System.out.println("------------------------");
}
public static String replaceMatches(String html){
//Pattern replace = Pattern.compile("\\s+");
Matcher matcher = COMPILED_PATTERN.matcher(html);
html = matcher.replaceAll(REPLACEMENT);
return html;
}
}
So, my method findMatches(String html) seems to find correctly all IMG tags where the src attributes starts with ./.
Now my method replaceMatches(String html) does not correctly replace the matches.
I am a newbie to regex, but I assume that either the REPLACEMENT regex is incorrect or the usage of the replaceAll method or both.
A you can see, the replacement String contains 2 parts which are identical in all IMG tags:
<img and src="./. In between these 2 parts, there should be the 0 or more HTML attributes from the original string.
How do I formulate such a REPLACEMENT string?
Can somebody please enlighten me?
Don't use regex for HTML. Use a parser, obtain the src attribute and replace it.
Try these:
PATTERN = "(<img[^>]*\\ssrc=\")\\./"
REPLACEMENT = "$1"
Basically, you capture everything except the ./ in group #1, then plug it back in using the $1 placeholder, effectively stripping off the ./.
Notice how I changed your .* to [^>]*, too. If there happened to be two IMG tags on the same line, like this:
<img src="good" /><img src="./bad" />
...your regex would match this:
<img src="good" /><img src="./
It would do that even if you used a non-greedy .*?. [^>]* makes sure the match is always contained within the one tag.
Your replacement is incorrect. It will replace the matched string by the replacement (not interpreted as a regexp). If you want to achieve, what you want, you need to use groups. A group is delimited by the parenthesis of the regexp. Each opening parenthesis indicates a new group.
You can use $i in the replacement string to reproduce what a groupe has matched and where 'i' is your group number reference. See The doc of appendReplacement for the details.
// Here is an example (it looks a bit like your case but not exactly)
String input = "<img name=\"foobar\" src=\"img.png\">";
String regexp = "<img(.+)src=\"[^\"]+\"(.*)>";
Matcher m = Pattern.compile(regexp).matcher(input);
StringBuffer sb = new StringBuffer();
while(m.find()) {
// Found a match!
// Append all chars before the match and then replaces the match by the
// replacement (the replacement refers to group 1 & 2 with $1 & $2
// which match respectively everything between '<img' and 'src' and,
// everything after the src value and the closing >
m.appendReplacement(sb, "<img$1src=\"something else\"$2>";
}
m.appendTail(sb);// No more match, we append the end of input
Hope this helps you
If src attributes only occur in your HTML within img tags, you can just do this:
input.replace("src=\"./", "src=\"")
You could also do this without java by using sed if you're using a *nix OS