Replace match and ignore certain characters - javascript

I want to replace {r-group1} with "REPLACED" but leave the , where it is.
So, the string
var string = "{r-group1, }foo bar"
should output: "REPLACED, foo bar"
Using a negative lookahead, I tried adding a preceding (?![,]) group to leave the comma alone:
var replaced = string.replace(^(?:(?![,]){r-group1\})+$, 'REPLACED');
But it returns the same string. There are no matches to replace.
The same goes for a preceding comma:
var string = "foo bar{r-, group1}"
This should output: "foo bar, REPLACED"

You could do the replacement without a lookahead. You could match the curly braces and the content that comes before and after it except a comma using a negated character class [^,}]+ and capture the comma with optional whitespace chars in a capturing group.
In the replacement use the capturing groups $1REPLACED$2
Credits to #Nick for the updated pattern.
{r-(,?\s*)[^,}]+(,?\s*)}
Regex demo
const regex = /{r-(,?\s*)[^,}]+(,?\s*)}/g;
const str = `{r-group1, }foo bar`;
const subst = `$1REPLACED$2`;
const result = str.replace(regex, subst);
console.log(result);

Related

Regex get string between ()

I have the text
var text = (hello) world this is (hi) text
I want to write a regex function so I can get
parseText(text) // returns ['hello', 'hi']
I tried this but not work:
'(hello) world this is (hi) text'.match('((.*?))')
Thanks for your help
you can try with:
/\([^\)]+\)/g
\(: escaped char
[^\)]+: one or more character(including symbols) until ) char.
\): escaped char
g flag: search all coincidences
const regex = /\([^\)]+\)/g;
const str = `(hello) world this is (hi) text`;
console.log(
str.match(regex) // this returns an string array
.map(i => i.slice(1, -1)) // remove first and last char
);
TIPS:
About point #2, you can change to [\)]* to take effect over zero
or more character.
If you need only string, you can use \w+ or \w*.
If you need only words you can use /\(\b\w+\b\)/g
You can find several options in this post.
Apart from using groups or postprocessing of the match results, you can use single regex match using lookahead / lookbehind:
var text = " (hello) world this is (hi) text"
var output = text.match(/(?<=\().*?(?=\))/g)
console.log(output)
output:
[ 'hello', 'hi' ]
Explanation:
(?<=...) ... positive lookbehind. The match is preceded be ..., but the ... is not included in the match
(?<=\() ... positive lookbehind for ( character
.* ... zero or more times of any character
.*? ... nongreedy version of .*
(?=...) ... positive lookahead, the match is followed by ... but the ... is not included in the match
(?=\)) ... positive lookahead for ) character
/.../g ... g is global flag, match finds all, not only the first, occurrence
do not forget to escape "special characters", e.g. parentheses
'(hello) world this is (hi) text'.match(/\([\w]*\)/g)
This returns [ "(hello)", "(hi)" ] and you can run another parse function to remove that extra parenthesis.
const text = '(hello) world this is (hi) text';
const list = text.match(/\([\w]*\)/g);
const parsed = list.map(item => item.replace(/\(|\)/g, ''));
console.log(parsed);

Regex for picking a Value After “=”

How i can select RQR-1BN6Q360090-0001 (without quotes) using Regex in below -
<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>
I tried this but it does not work
RptNum=([A-Za-z]+)$
You may use
/RptNum=([\w-]+)/
The pattern will match RptNum= and then capture 1 or more occurrences of word chars (letters, digits and _) or hyphens. See the regex demo and the regex graph:
Note that
/RptNum=([A-Z0-9-]+)/
might be a more restrictive pattern that should work, too. It does not match _ and lowercase letters.
In JS, use it with String#match() and grab the second array item upon a match:
var s = 'Object moved to here';
var m = s.match(/RptNum=([\w-]+)/);
if (m) {
console.log(m[1]);
}
Here, we can also use an expression that collects the new lines, such as:
[\s\S]*RptNum=(.+?)"[\s\S]*
[\w\W]*RptNum=(.+?)"[\w\W]*
[\d\D]*RptNum=(.+?)"[\d\D]*
and our desired output is saved in (.+?).
Test
const regex = /[\s\S]*RptNum=(.+?)"[\s\S]*/gm;
const str = `<html><head><title>Object moved</title></head><body>
<h2>Object moved to here.</h2>
</body></html>`;
const subst = `$1`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
Demo
RegEx
If this expression wasn't desired, it can be modified/changed in regex101.com.
RegEx Circuit
jex.im visualizes regular expressions:
const text = 'RptNum=RQR-1BN6Q360090-0001';
console.log(text.match(/RptNum=.*/).map(m => m.match(/RptNum=.*/)[0])[0].split('RptNum=')[1]);
I suppose that works

javascript extract hashtags from strings

I have a string received from backend, and I need to extract hashtags. The tags are written in one of these two forms
type 1. #World is a #good #place to #live.
type 2. #World#place#live.
I managed to extract from first type by : str.replace(/#(\S*)/g
how can i change the second format to space seperated tags as well as format one?
basically i want format two to be converted from
#World#place#live.
to
#World #place #live.
You can use String.match, with regex #\w+:
var str = `
type 1. #World is a #good #place to #live.
type 2. #World#place#live.`
var matches = str.match(/#\w+/g)
console.log(matches)
\w+ matches any word character [a-zA-Z0-9_] more than once, so you might want to tweak that.
Once you have the matches in an array you can rearrange them to your likes.
The pattern #(\S*) will match a # followed by 0+ times a non whitespace character in a captured group. That would match a single # as well. The string #World#place#live. contains no whitespace character so the whole string will be matched.
You could match them instead by using a negated character class. Match #, followed by a negated character class that matches not a # or a whitespace character.
#[^#\s]+
Regex demo
const strings = [
"#World is a #good #place to #live.",
"#World#place#live."
];
let pattern = /#[^#\s]+/g;
strings.forEach(s => {
console.log(s.match(pattern));
});
How about that using regex /#([\w]+\b)/gm and join by space like below to extract #hastags from your string? OR you can use str.replace(/\b#[^\s#]+/g, " $&") as commented by #Wiktor
function findHashTags(str) {
var regex = /#([\w]+\b)/gm;
var matches = [];
var match;
while ((match = regex.exec(str))) {
matches.push(match[0]);
}
return matches;
}
let str1 = "#World is a #good #place to #live."
let str2 = "#World#place#live";
let res1 = findHashTags(str1);
let res2 = findHashTags(str2);
console.log(res1.join(' '));
console.log(res2.join(' '));

exec from regex returns null

This is my code snippet:
var myString = '#EXTINF:-1 group-title="|FR| CHAINES FRANÇAISES |FR|",|FR|*****CANALSAT*****|FR|';
var group_title = /(group-title=")(\S*)["]/;
var matchgroup_title = group_title.exec(myString);
console.log(matchgroup_title);
I am not familiar to regex so I can't understand this code. Why does exec here return null?
The problem exists because of \S*. If you pop it into regex101.com, you'll see:
So, \s* will match any non-whitespace character, and a space is considered a whitespace character, so this does not match. You can simply use [^"] to check for anything that isn't another quote.
You can simplify this to:
var myString = '#EXTINF:-1 group-title="|FR| CHAINES FRANÇAISES |FR|",|FR|*****CANALSAT*****|FR|';
var group_title = /group-title="([^"]+)"/;
var matchgroup_title = group_title.exec(myString);
console.log(matchgroup_title);
(group-title=") matches group-title="
(\S*) matches zero or more non-whitespace characters, so |FR|
["] then matches a ", but there isn't one (well, there is, but there are whitespace characters first, so no match).

regex: Match when between text but not between digits

Please help.
I need a regular expression (to be used in javascript) to replace "." with "#" in a text containing Unicode characters.
Replacement takes place only when "." appears between text but not between digits.
Input: "ΦΨ. ABC. DEF. 123.456"
Desired output: "ΦΨ# ABC# DEF# 123.456"
Any suggestions?
You can use capturing groups in the regex and use back-references to obtain the required result:
var re = /(\D)\.(\D)/g;
var str = 'ΦΨ. ABC. DEF. 123.456';
var subst = '$1#$2';
result = str.replace(re, subst);
alert(result);
Regex Explanation:
\D - A non-digit character
\. - A literal dot
The non-digit characters are captured into groups, and then inserted back with the help of $1 and $2 back-references.
try this:
var str = "ΦΨ. ABC. DEF. 123.456";
str.replace(/[^\d.]+\.[^\d]/g, function (m) {
return m.replace('.', '#')
});

Categories