match text between comment string in javascript code - javascript

I am trying to strip out code between some tags. Its from a JavaScript plugin and it has multiple occurencies.
For example:
/*<ltIE8>*/ ╗
if (!item.hasOwnProperty) return false; ╣ this should match / go away
/*</ltIE8>*/ ╝
return item instanceof object; // this should not go away/match
...
/*<ltIE8>*/ ╗
if (!window.addEvenetListener) return false; ╣ this should match / go away
/*</ltIE8>*/ ╝
return window.addEvent;
I would like to match/remove those two blocks.
Tried using lookaheads like \/\*<ltIE8>\*\/(?!=\/\*<\/ltIE8>\*\/)([\s\S]+) but it ends up matching from the first ocurrence to the last, and missing the ones in-between.
Example: https://regex101.com/r/iD6mL8/1
Any sugestions? (I will be doing these replacements using JavaScript/NodeJS).

\/\*<ltIE8>\*\/([\s\S]+?)(?=\/\*<\/ltIE8>\*\/)
Try this.See demo.
https://www.regex101.com/r/fG5pZ8/18

I suck at regex, but this seems to work:
(\/\*<ltIE8>\*\/)[\s\S]*?(\/\*<\/ltIE8>\*\/)
The key to this solution is the ?, it tells the regex to not be greedy which basically means it stops when it finds the next /*</ltIE8>*/ rather than going all the way to the very last one.
Here is a working example

Related

How to delete the last occurence of the match?

I have the lyrics in the format like:
[00:26.8]Lo [00:27.0]rem[00:27.2] Ipsum[00:27.4] sam[00:27.6]ple [00:27.9]text[00:28.1] to [00:28.5]
[00:28.51]demonstrate[00:28.7] the[00:28.9] lyrics[00:29.1] text
I use the following regex to match time tags ([hh:mm.ss]):
/\[\d{1,2}:\d{1,2}\.\d{0,2}\]/ig
But how can I find and delete the last time tag ([00:29.1] in the example above)? Understand that I can match all occurences, take the last one, find the position of according tag within the text (with lastIndexOf usage), then delete the tag. But is there any better way to achieve it?
Upd. There is one more condition - if the time tag is at the beginning of the line, then it shouldn't be removed. I.e. in case of the lyrics:
[00:26.8]Lo [00:27.0]rem[00:27.2] Ipsum[00:27.4] sam[00:27.6]ple [00:27.9]text[00:28.1] to [00:28.5]
[00:28.51]demonstrate
The tag found and deleted should be [00:28.5], not [00:28.51].
Add a look ahead assertion to ensure that not [..] follows the matched string as
/\[\d{1,2}:\d{1,2}\.\d{0,2}\](?!(.|\n)*\[)/
Regex Demo
Also can do this without lookahead by adding a greedy [^]* or [\s\S]* before to eat up.
var str = str.replace(/^([^]*)(\[\d{1,2}:\d{1,2}\.\d{0,2}\])/, "$1");
Replace with captured first part. See fiddle
Ad update: add a [^\n] before:
var str = str.replace(/^([\s\S]*[^\n])(\[\d{1,2}:\d{1,2}\.\d{0,2}\])/, "$1");
See fiddle
The following should do (It makes sure the wanted [hh:mm.ss] isnt followed by any other opening or closing bracket till the end of the string... wich means that it's the last one you're looking for):
(\[\d{1,2}:\d{1,2}\.\d{0,2}\])(?=[^\]\[]*$)
DEMO

I want to find numbers-include dot and comma- but Regex not working in javascript

I thought it was very simple to find out. But how many ways I tried still not work properly.
Below is the test snippet.
"100$ and 1.000,000EUR 1,00.0.000USD .90000000000000000000$ (09898)".replace(/[\.,\d]*/g, '{n}')
And I want the result like below.
{n}$ and {n}EUR {n}USD {n}$ ({n})
The * is your problem, change the regex to /[.,\d]+/g instead.
"100$ and 1.000,000EUR 1,00.0.000USD .90000000000000000000$ (09898)".replace(/[.,\d]+/g, '{n}');
Output
{n}$ and {n}EUR {n}USD {n}$ ({n})
JSFiddle Example Check console screen for the output.
The problem here is that [\.,\d]* can match an empty string. The first step would be to use [.,\d]+ so that at least one of these characters matches.
But a better regex would be \d[.,\d]* because it ensures the replaced characters begin with a digit, so it won't replace periods in sentences.
If you want to go further, you can also use (?=[.,\d]*\d)[.,\d]+ if to handle numbers starting with periods. This one would be the proper answer for your case. The lookahead ensures there's at least one digit anywhere in the replaced text.
Note that you don't need to escape the . inside a character class.
\.?\d[^\s]*\d
Try this.Replace with {n}.See demo.
http://regex101.com/r/kP8uF5/3
var re = /\.?\d[^\s]*\d/gm;
var str = '100$ and 1.000,000EUR 1,00.0.000USD .90000000000000000000$ (09898)';
var subst = '{n}';
var result = str.replace(re, subst);

How would I make a regular expression that would mean /some string/ either followed by a line break or not?

function(input){
return input.replace(/teststring/ig, "adifferentstring");
}
I want to replace "teststring" and "teststring\n" with "adifferentstring"
In regex, to match a specific character you can place it in brackets:
[\n]
To make the match "optional", you can follow it with a ?:
[\n]?
In your exact example, your full regex could be:
teststring[\n]?
So, your function would look like:
function replace(input) {
return input.replace(/teststring[\n]?/ig, "adifferentstring");
}
I'd suggest going with matching characters in brackets as this makes for easy expansion; consider, for instance, that you want to match Window's newlines (a carriage-return + a newline):
teststring[\r\n]?
Try
function(input){
return input.replace(/teststring\n?/ig, "adifferentstring");
}
Try .replace(/teststring[\n]?/ig,"adifferentstring");
It would be something like this:
var re = /teststring([\n]?)/ig;
So then your replace statement would look about like this:
return input.replace(re,"adifferentstring");
Here's a fiddle showing the regex works.
And then a fiddle showing the replace operation working.
Edit:
Actually, thinking about the problem a little further, if your regex does match a carriage return or new line character, that would need to get put back into the replacing string. The same regex I posted originally will work but you will need this replace statement instead (with the $1 denoting the first group in parantheses.
return input.replace(re,"adifferentstring$1");
fiddle

JavaScript RegEx Match Failing

I am having issues matching a string using regex in javascript. I am trying to get everything up to the word "at". I am using the following and while it doesn't return any errors, it also doesn't do anything either.
var str = "Team A at Team B";
var matches = str.match(/(.*?)(?=at|$)/);
I tried multiple regex patterns before coming across this SO post, Regex to capture everything before first optional string, but it doesn't to return what I want.
Remove the ? at your first capturing group, and |$ from your second, and add ^ to mark beginning of string:
str.match(/^(.*)(?=at)/)
Alternatively (I personally find below easier to read, but your call):
str.substr(0, str.search(/\bat\b/))

Javascript RegEx match problem

I have a sentence structure along the lines of
[word1]{word2} is going to the [word3]{word4}
I'm trying to use a javascript regex to match the words for replacement later. To do this, I'm working towards getting the following multi-dimensional array:
[["word1", "word2"],["word3","word4"]]
I'm currently using this regex for the job:
\[(.*?)\]\{(.*?)\}
However, it comes up with results like:
["[word1]{word2}", "word1", "word2"]
or worse. I don't really understand why because this regex seems to work in Ruby just fine, and I'm not really much of a regex expert in general to understand what's going on. I'm just curious if there are any javascript rege expert's out there to whom this answer is very clear and can guide me along with what's going on here. I appreciate any help!
Edit:
This is the code I'm using just to test the matching:
function convertText(stringText) {
var regex = /\[(.*?)\]\{(.*?)\}/;
console.log(stringText.match(regex));
}
I assume you are using the exec method of the regular expression.
What you are doing is almost correct. exec returns an array where the first element is the entire match and the remaining elements are the groups. You want only the elements at indexes 1 and 2. Try something like this, but of course store the results into an array instead of using an alert:
var string = '[word1]{word2} is going to the [word3]{word4}';
var pattern = /\[(.*?)\]\{(.*?)\}/g;
var m;
while(m = pattern.exec(string)) {
alert(m[1] + ',' + m[2]);
}
This displays two alerts:
word1,word2
word3,word4
What you're seeing is Japanese hiragana. Make sure your input is in English maybe?
Edited to say: Upon further review, it looks like a dictionary entry in Japanese. The 私 is kanji and the わたし is hiragana, a phonetic pronunciation of the kanji. FWIW, the word is "Watashi" which is one of the words for "I" (oneself) in Japanese.

Categories