JavaScript String replace vs replaceAll

From gleaning the documentation for replaceAll, we find the following tidbits: const newStr = str.replaceAll(regexp|substr, newSubstr|function) Note: When using a regexp you have to set the global (“g”) flag; otherwise, it will throw a TypeError: “replaceAll must be called with a global RegExp”. In other words, when calling replaceAll with a regex literal or RegExp, … Read more

String replace a Backslash

sSource = sSource.replace(“\\/”, “https://stackoverflow.com/”); String is immutable – each method you invoke on it does not change its state. It returns a new instance holding the new state instead. So you have to assign the new value to a variable (it can be the same variable) replaceAll(..) uses regex. You don’t need that.

Java regex replaceAll multiline

You need to use the Pattern.DOTALL flag to say that the dot should match newlines. e.g. Pattern.compile(regex, Pattern.DOTALL).matcher(testIllegal).replaceAll(“x”) or alternatively specify the flag in the pattern using (?s) e.g. String regex = “(?s)\\s*/\\*.*\\*/”;

String replaceAll() vs. Matcher replaceAll() (Performance differences)

According to the documentation for String.replaceAll, it has the following to say about calling the method: An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl) Therefore, it can be expected the performance between invoking the String.replaceAll, and explicitly creating a Matcher and Pattern should be … Read more

Removing all fraction symbols like “¼” and “½” from a string

Fraction symbols like ¼ and ½ belong to Unicode Category Number, Other [No]. If you are ok with eliminating all 676 characters in that group, you can use the following regular expression: itemName = itemName.replaceAll(“\\p{No}+”, “”); If not, you can always list them explicitly: // As characters (requires UTF-8 source file encoding) itemName = itemName.replaceAll(“[¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞↉]+”, … Read more

replace \n and \r\n with in java

It works for me. public class Program { public static void main(String[] args) { String str = “This is a string.\nThis is a long string.”; str = str.replaceAll(“(\r\n|\n)”, “<br />”); System.out.println(str); } } Result: This is a string.<br />This is a long string. Your problem is somewhere else.

String.replaceAll single backslashes with double backslashes

The String#replaceAll() interprets the argument as a regular expression. The \ is an escape character in both String and regex. You need to double-escape it for regex: string.replaceAll(“\\\\”, “\\\\\\\\”); But you don’t necessarily need regex for this, simply because you want an exact character-by-character replacement and you don’t need patterns here. So String#replace() should suffice: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)