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

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);

Related

javascript - replace() delete all the repeat letter in a string [duplicate]

This question already has answers here:
Remove consecutive duplicate characters in a string javascript
(5 answers)
Closed 2 years ago.
I want to delete all the repeat letter in a string with javascript.
mot = "message in a bottle";
mot = mot.replace(/[\w]{2,}/i, '');
console.log(mot)
// result wish : "mesage in a botle"`
Thank you for your help.
You can group the word characters and then use a backreference to check if the same character follows that grouped character (with \1). You can replace all found occurrences with the group using $1. Ensure that you use the global flag (/g) to match all occurrences:
const mot = "message in a bottle";
const res = mot.replace(/(\w)\1/ig, '$1');
console.log(res);

Split by word not contained in parentheses [duplicate]

This question already has answers here:
How to split by commas that are not within parentheses?
(6 answers)
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 3 years ago.
Here is the my string
var string = 'Title:(India OR America) OR Keyword:(Education)';
After splitting with "OR" it is showing
["Title:(India", "America)", "Keyword:(Education)"]
But what I need is the below one.
["Title:(India OR America)", "Keyword:(Education)"]
Could anyone please help on how to split the string to get the required result.
To achieve this you'll need to use a negative lookahead to only find the OR string where it's not contained in parentheses. Try this:
var input = 'Title:(India OR America) OR Keyword:(Education)';
var output = input.split(/(?!\(.*)\s?OR\s?(?![^(]*?\))/g);
console.log(output);

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, ''));

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

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

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.

Categories

Resources