how to replace &,<,> in javascript [duplicate] - javascript

This question already has answers here:
Escaping HTML strings with jQuery
(27 answers)
Closed 5 years ago.
In javascript how it's possible to replace all occurrence of &,<,> in a string
I tried this
var str="<>&";
str.replace("&","&amp").replace("<","&lt").replace(">","&gt");
but not able to change even first occurrence

The easiest would be to use a regular expression with g flag to replace all instances:
str.replace(/foo/g, "bar")
This will replace all occurrences of foo with bar in the string str. If you just have a string, you can convert it to a RegExp object like this:
var pattern = "foobar",
re = new RegExp(pattern, "g");

Related

Regex: how to split "a.b" to "a" ".b" in javascript [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Closed 2 years ago.
I saw this Regex replace `a.b` to `a. b`? I don't understand it's for Python not for Javascript or it isn't well explained.
I know how to split "a.b" to ["a" "." "b"] with
regex = /(\.)/;
test = "a.b";
results = test.split(regex);
I can't see what regex to get
["a" ".b"]
You could split by the positive lookahead of a dot.
var string = "a.b",
result = string.split(/(?=\.)/);
console.log(result);

How to use replaceAll() in JavaScript? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
I have string and there are alot of character which is "\" so I want to remove all of them but when I use
string.replace("\\","");
it removes just first character. There is no replaceAll in reactnative. How can I do that ?
Use regex in your replace.
Example:
const string = "aaabbbcccaaa";
// Removes first "a" only
console.log(string.replace("a", ""));
// Removes all "a"
console.log(string.replace(/a/g, ""));
You have to use a regex with the g modifier whichs means globally, that will apply the regex as many times as necessary. You can use a tool like this to build and test your regex.
console.log('hello\\world\\'.replace(/\\/g, ''));

replace all occurence of a string with a character using javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 8 years ago.
I wan to replace all occurrence of a string with single quote but with str.replace it only replaces the first occurrence of the script:
"7<singleQuote>1 inche<singleQuote>s"
Code
var data = "7<singleQuote>1 inche<singleQuote>s"
var output = data.replace("<singleQuote>","'")
Output: 7'1 inche<singleQuote>s
I want to replace <singleQuote> with '.
Use regex with g flag:
var output = data.replace(/<singleQuote>/g, "'");
MDN: String.prototype.replace.

JavaScript regex for string literal? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a RegExp.escape function in Javascript?
I'm currently using: var keywords = new RegExp(req.params.keywords, 'i');
The catch is, if req.params.keywords == '.*', this will match anything, what I want is for it to match .* literally, as in \.\*\
Is there a more elegant solution than escaping every passed single character with a \?
If you want to match literally, instead of using the regular expressions included in the string, don't use a regular expression. Use the string indexOf() function to see if a string is contained withing another one.
For case insensitive matching, you convert each string to, say, lower case before the match.
var searchForString = req.params.keywords.toLowerCase();
var searchInString = xxx.toLowerCase();
if (searchInString.indexOf(searchForString) >= 0) {
... then it matches ...
}

Javascript Regular Expression to match six-digit number [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I am trying to incorporate a regular expression i have used in the past in a different manner into some validation checking through JavaScript.
The following is my script:
var regOrderNo = new RegExp("\d{6}");
var order_no = $("input[name='txtordernumber']").val();
alert(regOrderNo.test(order_no));
Why would this not come back with true if the txtordernumber text box value was a six digit number or more?
You have to escape your \ when used inside a string.
new RegExp("\\d{6}");
or
/\d{6}/
Insert an extra "\" in your regexp.
You need to escape your backslash. It's looking for "\d", not digits.
So...
var regOrderNo = new RegExp("\\d{6}");

Categories

Resources