Skip to content

Instantly share code, notes, and snippets.

@nyilmaz
Created June 19, 2014 08:50
Show Gist options
  • Save nyilmaz/b5ce1ece6e284c5e4fe1 to your computer and use it in GitHub Desktop.
Save nyilmaz/b5ce1ece6e284c5e4fe1 to your computer and use it in GitHub Desktop.
private static String escapeSeleniumXpathString(String toBeEscaped) {
Pattern pattern = Pattern.compile("[^'\"]+|['\"]");
Matcher matcher = pattern.matcher(toBeEscaped);
final List<String> replaced = Lists.newArrayList();
while(matcher.find()) {
replaced.add(matcher.group());
}
return "concat(" + StringUtils.collectionToCommaDelimitedString(FluentIterable.from(replaced).transform(new Function<String, String>() {
@Override
public String apply(String input) {
if(input.equals("'")) {
return "\"'\"";
}
if(input.equals("\"")) {
return "'\"'";
}
return "'" + input + "'";
}
}).toList()) + ")";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment