How to remove characters from string using regex - javascript

I want to remove << any words #_ from the following string.
stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ ";
Result mast be:
Start words I love kind <<man>>, john <<kind man>> is really <<great>> end words
I tried like this:
stringVal = stringVal.replace(/^.*<<.+\#_.*$/g, "");
But it removes all string.
Note: << any words #_ may exists multiple time in string, at the start, in the middle or at the end

Inferring from your examples, you might be looking for:
stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ ";
stringVal = stringVal.replace(/<<([-\w ]+)#_/g, "$1");
console.log(stringVal);
To allow other characters, change the \w+ to e.g. [-\w ]+.
See an additional demo on regex101.com.

Instead of using .+\#_, and you want to match any words you could match word characters optionally repeated by matching a space space and word characters.
<<(\w+(?: \w+)*)#_
Regex demo
In the replacement use group 1 $1
Note that you don't have to escape #
const regex = /<<(\w+(?: \w+)*)#_/g;
stringVal = "I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>>";
const result = stringVal.replace(regex, '$1');
console.log(result);

Related

remove last part of string following '&&&' with JavaScript Regex

I'm trying to use a regex in JS to remove the last part of a string. This substring starts with &&&, is followed by something not &&&, and ends with .pdf.
So, for example, the final regex should take a string like:
parent&&&child&&&grandchild.pdf
and match
parent&&&child
I'm not that great with regex's, so my best effort has been something like:
.*?(?:&&&.*\.pdf)
Which matches the whole string. Can anyone help me out?
You may use this greedy regex either in replace or in match:
var s = 'parent&&&child&&&grandchild.pdf';
// using replace
var r = s.replace(/(.*)&&&.*\.pdf$/, '$1');
console.log(r);
//=> parent&&&child
// using match
var m = s.match(/(.*)&&&.*\.pdf$/)
if (m) {
console.log(m[1]);
//=> parent&&&child
}
By using greedy pattern .* before &&& we make sure to match **last instance of &&& in input.
You want to remove the last portion, so replace it
var str = "parent&&&child&&&grandchild.pdf"
var result = str.replace(/&&&[^&]+\.pdf$/, '')
console.log(result)

Java Script - Regular Expression matching a word in a string

I am trying to find a match in a string with JavaScript. I want to work with the RegEx function.
My example (what I have tried):
var str = "hello.you";
var patt1 = '\\b' + str + '\\b';
var result = str.match(patt1);
But this does not give me the result which I except. I just want to print "you".
Thanks all in advance.
So you jumped right into a pretty advanced regex topic. You sort of want to do a lookahead (the word AFTER a given boundary character). The following will get you there:
let str = "hello.you",
myRegex = /(?<=\.)\w+/;
let theWord = str.match(myRegex);
console.log(theWord[0]);
... And what that does, is uses (?<=.) to indicate "something that comes after a period", followed by \w+ to indicate a word.
I'd recommend using a regex tester, and build from that. I use https://www.regextester.com/

JS conditional RegEx that removes different parts of a string between two delimiters

I have a string of text with HTML line breaks. Some of the <br> immediately follow a number between two delimiters «...» and some do not.
Here's the string:
var str = ("«1»<br>«2»some text<br>«3»<br>«4»more text<br>«5»<br>«6»even more text<br>");
I’m looking for a conditional regex that’ll remove the number and delimiters (ex. «1») as well as the line break itself without removing all of the line breaks in the string.
So for instance, at the beginning of my example string, when the script encounters »<br> it’ll remove everything between and including the first « to the left, to »<br> (ex. «1»<br>). However it would not remove «2»some text<br>.
I’ve had some help removing the entire number/delimiters (ex. «1») using the following:
var regex = new RegExp(UsedKeys.join('|'), 'g');
var nextStr = str.replace(/«[^»]*»/g, " ");
I sure hope that makes sense.
Just to be super clear, when the string is rendered in a browser, I’d like to go from this…
«1»
«2»some text
«3»
«4»more text
«5»
«6»even more text
To this…
«2»some text
«4»more text
«6»even more text
Many thanks!
Maybe I'm missing a subtlety here, if so I apologize. But it seems that you can just replace with the regex: /«\d+»<br>/g. This will replace all occurrences of a number between « & » followed by <br>
var str = "«1»<br>«2»some text<br>«3»<br>«4»more text<br>«5»<br>«6»even more text<br>"
var newStr = str.replace(/«\d+»<br>/g, '')
console.log(newStr)
To match letters and digits you can use \w instead of \d
var str = "«a»<br>«b»some text<br>«hel»<br>«4»more text<br>«5»<br>«6»even more text<br>"
var newStr = str.replace(/«\w+?»<br>/g, '')
console.log(newStr)
This snippet assumes that the input within the brackets will always be a number but I think it solves the problem you're trying to solve.
const str = "«1»<br>«2»some text<br>«3»<br>«4»more text<br>«5»<br>«6»even more text<br>";
console.log(str.replace(/(«(\d+)»<br>)/g, ""));
/(«(\d+)»<br>)/g
«(\d+)» Will match any brackets containing 1 or more digits in a row
If you would prefer to match alphanumeric you could use «(\w+)» or for any characters including symbols you could use «([^»]+)»
<br> Will match a line break
//g Matches globally so that it can find every instance of the substring
Basically we are only removing the bracketed numbers if they are immediately followed by a line break.

Javascript regex between string delimiters

I have the following string:
%||1234567890||Joe||% some text winter is coming %||1234567890||Robert||%
PROBLEM: I am trying to match all occurrences between %||....||% and process those substring matches
MY REGEX: /%([\s\S]*?)(?=%)/g
MY CODE
var a = "%||1234567890||Joe||% some text winter is coming %||1234567890||Robert||%";
var pattern = /%([\s\S]*?)(?=%)/g;
a.replace( pattern, function replacer(match){
return match.doSomething();
} );
Now the patterns seems to be selecting the everything between the first and last occurrence of %|| .... %||
MY
FIDDLE
WHAT I NEED:
I want to iterate over the matches
%||1234567890||Joe||%
AND
%||1234567890||Robert||%
and do something
You need to use a callback inside a String#replace and modify the pattern to only match what is inside %|| and ||% like this:
var a = "%||1234567890||Joe||% some text winter is coming %||1234567890||Robert||%";
var pattern = /%\|\|([\s\S]*?)\|\|%/g;
a = a.replace( pattern, function (match, group1){
var chunks = group1.split('||');
return "{1}" + chunks.join("-") + "{/1}";
} );
console.log(a);
The /%\|\|([\s\S]*?)\|\|%/g pattern will match:
%\|\| - a %|| substring
([\s\S]*?) - Capturing group 1 matching any 0+ chars as few as possible up to the first...
\|\|% - a ||% substring
/g - multiple times.
Because he tries to take as much as possible, and [\s\S] basically means "anything". So he takes anything.
RegExp parts without escaping, exploded for readability
start tag : %||
first info: ([^|]*) // will stop at the first |
separator : ||
last info : ([^|]*) // will stop at the first |
end tag : ||%
Escaped RegExp:
/%\|\|([^\|]*)\|\|([^\|]*)\|\|%/g

Remove specific words except last in JavaScript?

I have a sentence that I would like to have only the last 'and' remaining, and remove the others.
"Lions, and tigers, and bears, and elephants", and I would like to turn this into:
"Lions, tigers, bears, and elephants".
I have tried using a regex pattern like str = str.replace(/and([^and]*)$/, '$1'); which obviously didn't work. Thanks.
Use this regex:
and (?=.*and)
and matches any and followed by a space. Space is matched so it is removed on replacement, to prevent having 2 spaces
(?=.*and) is a lookahead, meaning it will only match if followed by .*and, if followed by and
Use this code:
str = str.replace(/and (?=.*and)/g, '');
You can use a positive look-ahead (?=...), to see if there is another and ahead of the current match. You also need to make the regex global with g.
function removeAllButLastAnd(str) {
return str.replace(/and\s?(?=.*and)/g, '');
}
console.log(removeAllButLastAnd("Lions, and tigers, and bears, and elephants"));
var multipleAnd = "Lions, and tigers, and bears, and elephants";
var lastAndIndex = multipleAnd.lastIndexOf(' and');
var onlyLastAnd = multipleAnd.substring(0, lastAndIndex).replace(/ and/gi, '')+multipleAnd.substring(lastAndIndex);
console.log(onlyLastAnd);

Categories

Resources