Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I tried .replace(/ *\[[^)]*\] */g, ""); and it works for instances where there's only a pair of brackets
"[Dialog4]Hello, this is Mike"
but doesn't work for
"[Dialog4]Hello, this is Mike[Dialog5]"
because it just removes entire thing
The result should be
"Hello, this is Mike"
use not greedy mode in regex:
\[.*?\]
here is a tester: https://regex101.com/r/NyireC/1
You can use
\[[^\]]*\]
let str = "[Dialog4]Hello, this is Mike[Dialog5]"
let replaced = str.replace(/\[[^\]]*\]/g,"")
console.log(replaced)
Your regex is almost there.
You don't need the space+* at the start and end, because you only want to replace the square brackets and their contents, not anything before/after it.
In the negated character class, you are negating ), where you should be negating ] instead. This is possibly a typo.
With these modifications, the regex becomes:
\[[^\]]*\]
Demo
Perhaps a bit sloppy, but you could use the regex /\[(?<=\[)[^\]]*(?=\])]/g.
This makes use of both a positive lookbehind and positive lookahead, on the [ and ] characters respectively.
const string = "[Dialog4]Hello, this is Mike[Dialog5]";
const regex = /\[(?<=\[)[^\]]*(?=\])]/g;
const output = string.replace(regex, "");
console.log(output);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
How to replace the specific string only using one replace instead of two?
const formattedUrl = url.replace('flashget://', '').replace('&abc','')
What I have tried: (Not Working)
const formattedUrl = url.replace(/flashget:\/\/ | &abc/g, '').replace('&abc','')
Example
Input Url: flashget://W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=&abc
Formatted Url: W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=
Take out the spaces around the |
This is my attempt:
https://regex101.com/r/eFO7Eh/2
Search Regex:
flashget:\/\/(.*)\&.*$
Replace term:
$1
Just pay attention to the fact that this is a different logic and requires handling capture groups.
remove the space before and after the or |. it will work.
let url = "flashget://W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=&abc"
const formattedUrl = url.replace(/flashget:\/\/|&abc/g, '');
console.log(formattedUrl);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to match some characters in a text that contains \n\r:
`start[\n\r]other words end other words`.match(/start(?!end)./)
expect to:
start[\n\r]other words
but got:
start[
What's the problem, why I get this error result?
Thanks in advance!
It has nothing to do with the newline. Ignoring the look-ahead, your regex is only asking to match the word start and one additional character /start./, so that's exactly what you are getting. What you actually want is to match start and everthing up to, but not including, end. You can do that with start.*?(?=end).
You seem to want to match
everything until there is end, or
matching everything up to a point where it is not end to the right (so it'd one character less (1) above
In NodeJS:
console.log(`start[\n\r]other words end other words`.match(/(start.*?)end/s)[1])
// => 'start[\n\r]other words '
console.log(`start[\n\r]other words end other words`.match(/(start.*?).end/s)[1])
// => 'start[\n\r]other words'
The modifier s let the . match newline characters.
Note that
`start[\n\r]other words end other words`.match(/(start.*)(?!end)/s)[1]
// => 'start[\n\r]other words end other words'
because the result satisfied "matching start and any length of string, and then it doesn't have end to the right).
and
`start[\n\r]other words end other words`.match(/(start.*?)(?!end)/s)[1]
// => 'start'
satisfied "matching start and any length of string and don't be greedy, and then it doesn't have end to the right).
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I need to find a regex to extract first occurrence of a string from a data.
For example my data is like :
SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD
I need to extract ABCD123456789 from this data.
I need to find first occurrence of string always starts with ABCD and has total length of 13.
How can I achieve this with using regex?
I tried regex /^ABCD(\w{9})$/gm which didn't work for me.
You can use /ABCD\w{9}/g with match() to get the result from first index:
var str = "SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD"
console.log(str.match(/ABCD\w{9}/g)[0])
The pattern that you tried ^ABCD(\w{9})$ does not match because you use anchors ^ and $ to assert the start and the end of the string.
Note that if you want a full match only, you don't need a capturing group (\w{9})
You can omit those anchors, and if you want a single match from the string you can also omit the /g global flag and the /m multiline flag.
ABCD\w{9}
Regex demo
const regex = /ABCD\w{9}/;
const str = `SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD`;
console.log(str.match(regex)[0])
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
When I type in my input the text appears on image. I want to make the code work like that:
When user types character that is recognized by regex then place it on image else just throw some error or do not let it type at all in the input field.
This is the regex: [A-Z0-9a-z&,.-/()#*+!?"':; -]
I'm trying to achieve it like this:
$("#firstText").keyup(function () {
var value = $(this).val().toUpperCase();
var regex = "[A-Z0-9a-z&,.-/()#*+!?"':; -]";
if(regex.test(value))
{
$(".zetin16").text(value);
} else {
alert('this is bad');
}
});
But I get this error: Uncaught SyntaxError: Invalid or unexpected token
In this line: var regex = "[A-Z0-9a-z&,.-/()#*+!?"':; -]";
Thanks in advance for any help.
UPDATE
The regex working fine now. Now I want to prevent typing characters in input when regex doesnt match the character. This is my code currently:
$("#firstText").keyup(function(e) {
var value = $(this).val().toUpperCase();
var regex = new RegExp(/[A-Z0-9a-z&,.-/()#*+!?"':; -]/);
if (regex.test(value)) {
$(".zetin16").text(value);
} else {
e.preventDefault();
return false;
}
});
With regex, use the forward slash as delimiter. If a forward slash occurs as a literal inside the regex itself, escape it:
var regex = /[A-Z0-9a-z&,.-\/()#*+!?"':; -]/;
Reference: JavaScript regex replace - escaping slashes
(The problem with the original string was that it contained a double quote, and was delimited using double quotes at the same time).
The exact error you're seeing is because you are defining the variable as a double quoted string, with an unescaped double quote in it.
It shouldn't be a string anyway. It should be a regular expression like this.
var regex = /[A-Z0-9a-z&,.-/()#*+!?"':; -]/;
try using this pattern for using regular expression
var regex = "['A-Z0-9a-z&,.-/()#*+!?':; -]";
var reg =new RegExp(regex)
var val ="asss"
reg.test(val)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
var testString = "This string has a bad word in it to test";
function findBadWords(string) {
var badWord = /\bbad\b | \bword\b | \btest\b/gi
var isBadWord = string.match(badWord);
if (isBadWord) {
newString = string.replace(badWord," *** ");
}
document.write(newString);
}
findBadWords(testString);
So I'm practicing with RegExp's currently and I have run into a problem I don't understand. In the code above, I have set a RegExp to find "bad words" in a string. From what I can tell, I have set it to find the word "bad", "word", and "test" as long as there is a word boundary before and after the word. The issue I'm having is that "word" isn't being replaced. If I put a non-badWord before "word" it gets replaced, but not otherwise. I have tried taking off some of the word boundaries or adding some non-word boundaries with no luck. Would anyone mind explaining why this code is working the way that it is and how I could fix it?
Thanks!
Also, I know using document.write is a poor choice but it's only for testing I swear!
The issue here is the \b alongside the " " empty space character. If you remove the spaces from your regex it works well.
var testString = "This string has a bad word in it to test";
function findBadWords(string) {
var badWord = /\bbad\b|\bword\b|\btest\b/gi
var isBadWord = string.match(badWord);
if (isBadWord) {
newString = string.replace(badWord," *** ");
}
document.write(newString);
}
findBadWords(testString);