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).
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 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 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);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I would like to split the following str into an array that has 2 elements, the first being 'Some words in a sentence', the second being 'ABC' where ABC can be any upper case character.
const str = 'Some words in a sentence (ABC)';
const regex = ?
const arr = str.split(regex);
...
expect(arr[0]).to.eq('Some words in a sentence');
expect(arr[1]).to.eq('ABC');
expect(arr.length).to.eq(2);
Any thoughts would be appreciated.
Cheers,
Paul
The trick with regex is to break it into steps.
You want two elements, that means two groups, e.g. (<-- between these -->))
The easy one is getting the (ABC) at the end -> \((.*?)\)$ (escaped ()'s
And now you can conclude the rest is the other part: (.*?)
Explained:
/---\ is the \((.*?)\)$
Some words in a sentence (ABC)
^---- (.*?) ----^
That should push you in the direction you are looking for, the final part is for you to figure out :)
Goto https://regex101.com/ and try this
/(.*)\(([A-Z]{3})\)/
with your string
'Some words in a sentence (ABC)'
You will get
Match 1
Full match 0-30 `Some words in a sentence (ABC)`
Group 1. 0-25 `Some words in a sentence `
Group 2. 26-29 `ABC`
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);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've a strings, which can have a text like:
'some text user#t12# some text'
'username#John# some text'
'some text usersurname#Malks#'
'userphoto#1.jpg#'
How do I get a text between # and # symbols?
There's a typical structure of the part of the string to search for - type#variable#
type is a JS variable type, it's placed before the first #.
variable is a text that I need to get.
I'm searching for a regexp, that return variable, that is between #...#.
The problem is, I'm not too familiar with regexp, can you help me please?
You need to use capture groups, basically in a regex anything in brackets will be part of the cpature group, in this case you want to capture all the characters between two hashes. The any amount of characters regex is .* so this is what you want to capture between two hashes. Once you execute it you will find the match as second in the array (the first will be the string with the hashes.
var type = "";
var myString = "some text user#t12# some text";
var myRegexp = new RegExp(type+"#(.*)#","g");
var match = myRegexp.exec(myString);
alert(match[1]); // t12
any other matches between hashes will be in match[2].. match[n]