Is it possible to do a double replace? - javascript

If double replace is it possible to do?
var string = [link="<iframe"qwe"></iframe>"]
var output = string.replace(/[link="([^"]+)"]/g, '$1.replace(/"([^"]+)"/g, "'")');
what i want output:
[link="<iframe'qwe'></iframe>"]

You can use a function as the replacement in replace(). It can then do its own replace on the capture group.
var string = '[link="<iframe"qwe"></iframe>"]';
var output = string.replace(/link="([^\]]+)"]/g, (match, group1) =>
'link="' + group1.replace(/"/g, "'") + '"]');
console.log(output);
Note also that I had to correct your regexp. ([^"]+) should be ([^\]]+) so that it can match a string containing double quotes -- you need to capture that so you can replace the double quotes.
And in the second replacement, you want to match ", not [^"]+

Related

Replace multiple characters in a string using regex

I want to replace multiple occurrences of multiple characters in a string.Let's say I have a string which contains spaces and / characters. How can I replace both with _?
I know how to replace a single character with the replace() method as below:
var s = 'some+multi+word+string'.replace(/\+/g, ' ');
How can I update this to replace multiple characters?
To capture multiple consecutive characters use the + operator in the Regex. Also note that to capture spaces and slashes you can specify them in a character set, for example [\s\/]. Try this:
var s = 'some multi///word/string'.replace(/[\s\/]+/g, '_');
console.log(s);
Define your characters inside a list []
const r = "abc def/ //g h".replace(/[ \/]/g, "_");
console.log(r) // "abc_def____g_h"
To replace multiple, use the + quantifier (one or more occurrences)
const r = "abc def/ //g h".replace(/[ \/]+/g, "_");
console.log(r) // "abc_def_g_h"
To replace the exact sequence " /" (Space+ForwardSlash) with "_"
const r = "abc /def /g /h".replace(/ \/+/g, "_");
console.log(r) // "abc_def_g_h"

How to replace numbers with an empty char

i need to replace phone number in string on \n new line.
My string: Jhony Jhons,jhon#gmail.com,380967574366
I tried this:
var str = 'Jhony Jhons,jhon#gmail.com,380967574366'
var regex = /[0-9]/g;
var rec = str.trim().replace(regex, '\n').split(','); //Jhony Jhons,jhon#gmail.com,
Number replace on \n but after using e-mail extra comma is in the string need to remove it.
Finally my string should look like this:
Jhony Jhons,jhon#gmail.com\n
You can try this:
var str = 'Jhony Jhons,jhon#gmail.com,380967574366';
var regex = /,[0-9]+/g;
str.replace(regex, '\n');
The snippet above may output what you want, i.e. Jhony Jhons,jhon#gmail.com\n
There's a lot of ways to that, and this is so easy, so try this simple answer:-
var str = 'Jhony Jhons,jhon#gmail.com,380967574366';
var splitted = str.split(","); //split them by comma
splitted.pop(); //removes the last element
var rec = splitted.join() + '\n'; //join them
You need a regex to select the complete phone number and also the preceding comma. Your current regex selects each digit and replaces each one with an "\n", resulting in a lot of "\n" in the result. Also the regex does not match the comma.
Use the following regex:
var str = 'Jhony Jhons,jhon#gmail.com,380967574366'
var regex = /,[0-9]+$/;
// it replaces all consecutive digits with the condition at least one digit exists (the "[0-9]+" part)
// placed at the end of the string (the "$" part)
// and also the digits must be preceded by a comma (the "," part in the beginning);
// also no need for global flag (/g) because of the $ symbol (the end of the string) which can be matched only once
var rec = str.trim().replace(regex, '\n'); //the result will be this string: Jhony Jhons,jhon#gmail.com\n
var str = "Jhony Jhons,jhon#gmail.com,380967574366";
var result = str.replace(/,\d+/g,'\\n');
console.log(result)

Remove all special characters from string in JS

I want to remove all special characters from string and add only one "-"(hyphen) in the place.
Consider below example
var string = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.'
So, from the above string, if there is a continuous number of special characters then I want to remove all of them and add only one "-" or if there is a single or double special character then also that should be replaced by "-"
Result should be like this
Lorem-Ipsum-is-simply-dummy text-
I have tried below, but no luck
var newString = sourceString.replace(/[\. ,:-]+/g, "-");
You could use .replace to replace all non-alphabetical character substrings with -:
const input = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s]+/gi, '-');
console.log(output);
If you want to permit numbers too:
const input = 'Lorem123%^$&*&^654Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s\d]+/gi, '-');
console.log(output);

Match the occurance of a pattern within a string and insert characters in javascript?

For instance, assume we have a string firstName=&lastName=&phoneNumber=&. Now, we would like to match the pattern lastname= so that we can insert bobson into the space before the ampersand. Therefore, the final result would be firstName=&lastName=bobson&phoneNumber=&.
You could search for the pattern and insert
var string = 'firstName=&lastName=&phoneNumber=&',
replacement = 'bobson';
console.log(string.replace(/lastName=/, '$&' + replacement));
For replacement, you could seach for the pattern and replace all between pattern and ampersand.
var string = 'firstName=&lastName=xx&phoneNumber=&',
replacement = 'bobson';
console.log(string.replace(/(lastName=)[^&]*/, '$1' + replacement));

Replace specific character only when within a boundary

Replace specific character only when within a boundary.
For example replace html entity only when enclosed inside single quotes.
Input:
<i>Hello</i> '<i>How are you</i>'
Output:
<i>Hello</i> '<i>How are you</i>'
You can use replace with a callback:
var s = "<i>Hello</i> '<i>How are you</i>'";
var r = s.replace(/('[^']+')/g, function($0, $1) {
return $1.replace(/</g, '<').replace(/>/g, '>'); });
//=> <i>Hello</i> '<i>How are you</i&gt';
You'll need to use several regular expressions, first capture the text within single quotes, and then replace all occurrences of your character.
var input = "<i>Hello</i> '<i>How are you</i>'";
var quoted = input.match(/'.*'/)[0];
var output = quoted.replace("<", "<").replace(">", ">");
// output == "<i>Hello</i> '<i>How are you</i>'"

Categories

Resources