I had the following regex to match opening quotation marks:
/'(?=\b)/g
But then I realized that it also captures stuff like don't and it's.
So I added another rule:
/'(?=\b[^a-zA-Z])/g
Capture an opening quotation mark not followed by a letter (t and s in this case). But now none of the quotation marks are being highlighted.
Did I modify the regex in a wrong way?
EDIT:
Oh, I realized my dumb mistake. Anyway, here's what I want to do:
"two 'three'"
"four don't it's "
I want to match the opening ' in three but not the 's in don't and it's
You can try this regex to match quotes but skip those cases like don't it's etc:
/(?:^|[^a-zA-Z])'|'(?![a-zA-Z])/gm
RegEx Demo
You can use the following regex to only match opening '
/(?!\b)'/g
See the demo on your testcases
You have your look-around in the wrong place. You want to find instances where a quote is preceded by nothing, and proceeded by characters. This would probably work
/\b'\B/
Referring to your last EDIT, this Regex /[\.\,\;\?\!\s]+'/g is what you are looking for, it matches quotation in sentences like 'three' and skips what you need (sentences like don't and it's).
Here's a DEMO.
Related
I have a text and I need to match all text parts except given words with regexp
For example if text is ' Something went wrong and I could not do anything ' and given words are 'and' and 'not' then the result must be ['Something went wrong', 'I could', 'do anything']
Please don't advise me to use string.split() or string.replace() and etc. I know a several ways how I can do this with build-in methods. I'm wonder if there a regex which can do this, when I will execute text.math(/regexp/g)
Please note that the regular expression must work at least in Chrome, Firefox and Safari versions not lower than the current one by 3! At the moment of asking this question the actual versions are 100.0, 98.0.2 and 15.3 respectively. For example you can not use lookbehind feature in Safari
Please, before answering my question, go to https://regexr.com/ and check your answer!. Your regular expression should highlight all parts of a sentence, including spaces between words of need parts and except empty spaces around need parts, except for the given words
Before asking this question I tried to do my own search but this links didn't help me. I also tried non accepted answers:
Match everything except for specified strings
Regex: match everything but a specific pattern
Regex to match all words except a given list
Regex to match all words except a given list (2)
Need to find a regular expression for any word except word1 or word2
Matching all words except one
Javascript match eveything except given words
It's possible with only using match and lookaheads in javascript.
/\b(?=\w)(?!(?:and|not)\b).*?(?=\s+(?:and|not)\b|\s*$)/gi
Test on RegExr here
Basically match the start of a word that's not a restricted word
\b(?=\w)(?!(?:and|not)\b)
Then a lazy match till the next whitespaces and restricted word, or the end of the line without including last whitespaces.
.*?(?=\s+(?:and|not)\b|\s*$)
Test Snippet :
const re = /\b(?=\w)(?!(?:and|not)\b).*?(?=\s+(?:and|not)\b|\s*$)/gi
let str = ` Something went wrong and I could not do anything `;
let arr = str.match(re);
console.log(arr);
See Edit further down.
You can use this regex, which only use look ahead:
/(?!and|not)\b.*?(?=and|not|$)/g
Explanation:
(?!and|not) - negative look ahead for and or not
\b - match word boundary, to prevent matching nd and ot
.*? - match any char zero or more times, as few as possible
(?=and|not|$) - look ahead for and or not or end of text
If your text has multiple lines you can add the m flag (multiline). Alternatively you can replace dot (.) with [\s\S].
Edit:
I have changed it a little so spaces around the forbidden words are removed:
/(?!and|not)\b\w.*?(?= and| not|$)/g
I have added a \w character match to push the start of the match after the space and added spaces in the look ahead.
Edit2: (to handle multiple spaces around words):
You were very close! All you need is a \s* before the dollar sign and specified words:
/(?!and|not|\s)\b.*?(?=\s*(and|not|$))/g
Updated link: regexr.com
I'm creating a regex that matches straight apostrophes and replaces them with a curly ones. Sometimes an apostrophe goes in the middle of two characters. Other times goes at the end of a character/word (e.g. ellipsis').
So I have two regexes that handle both situations (separated by an or statement).
However, only the first case is being replaced, not the second. In other words, this:
"Wor'd word'".replace(/(?<=\w)\'(?=\w)|(?<=\w)\'(?=\s)/, '’')
Becomes this:
"Wor’d word'"
This confuses me because both types of apostrophes are matching: https://regexr.com/4td7p
Why is this, and how to fix it?
Update: I figured the problem was that there's no space after the last apostrophe, so I changed the second part of the regex to this: (?<=\w)\'(?!\w) (don't match if there's a character after the apostrophe). But I'm getting the same result.
If you want to match (?<=\w)\' followed by a character and also match (?<=\w)\' not followed by a character, why not just drop the logic after it altogether and just use (?<=\w)'? (no need to escape 's in a regex)
You also need the global flag to replace more than one thing at a time:
console.log(
"Wor'd word'".replace(/(?<=\w)'/g, '’')
);
updated
var str = "Wor'd word' that's a good thing'";
var afterReplace = str.replace(/'\b/g, '’')
console.log(afterReplace);
I just wanted to ask for an example of a string that would match this regular expression for JS:
/\/[a-z]{2,6}\/(\([0-9]+\)?$/
But this bit confuses me: /(\([0-9]+\)?$/
If I could get an example of a string using this regular expression and a brief explanation, that would be enough to clear it up for me.
Thanks!
EDITED: Sorry for the trouble, I missed a parentheses, however I just want to clear up, a string that would match would be such as:
/ab/(12345) or /abcdef/(1) etc right?
Here is a visual representation of the #Paul Roub explanation:
EDITED with last pattern
\/[a-z]{2,6}\/\([0-9]+\)$
Debuggex Demo
This example is OK : /abcdef/(12345)
\/
A forward slash (escaped), followed by
[a-z]{2,6}
between two and six lowercase English letters, followed by
\/
another slash
([0-9]+)?
the inside part - one or more digits. ? means "zero or one", so we're looking for a string of digits, or nothing. The parentheses would let this number be captured as a group for later processing
$
and the end of the string.
Things that would match:
/ab/0
/ab/
/acdefg/12345
things that would not match
/a/0
/abcdefgh/12345
/ab/0x
That regexp is malformed.
Opening bracket is escaped, but closing one not
Opening paren is not escaped but closing one is
I think the intended regexp was:
/\/[a-z]{2,6}\/([0-9]+)?$/
This would match:
/ab/1
/ab/
/abc/123
Wouldn't match:
/a/1
ab/1
/abcdefg/123
Cheers.
I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have
/^[\w\D]$/
which is not working :/
sorry guys, forgot to mention that will need to exclude all spec characters also.
You seem to want this one :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/
It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.
Example :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")
returns true
Your regular expression matches exactly one such character.
You can add the + modifier to match one or more characters.
To match a string consisting only of letters and whitespace characters, you can use:
/^[a-zA-Z\s]+$/
I have a string like this: ----------- 243f,33f----
Now I want to remove all the - chars except the first -, the , and the numbers. The result should be -243,33. How would I do this?
Your question still isn't very clear, but this yields the output you want:
'----------- 243f,33f----'.replace(/(?!^-)[^\d,]+/g, '')
The regex matches one or more of any character except digits or commas, after making sure the first character isn't a hyphen at the beginning of the string.
EDIT: To those who came up with regexes using (?<!^) and then withdrew them because JavaScript doesn't support lookbehinds, that wasn't necessary. I know there were other problems with those answers, but for the purpose of matching "not the beginning of the string", (?!^) works just fine.
'----------- 243f,33f----'.replace(/[^-0123456789,]/g, '').replace(/^-/, 'x').replace(/-/g, '').replace(/x/, '-')
output: '-243,33'
replace the substring from second character ala
string.charAt(0) + string.substring(1).replace("-","");
but i don't know if the syntax is correct
EDIT:
oh if you want to remove the f's too you can remove any nondigit and non-comma:
string.charAt(0) + string.substring(1).replace("[^0-9,]","");
Try this one:
(?:(^-)|[^\d,\r\n])
and replace with
$1
See it here on Regexr
Its a bit more difficult since Javascript does not support look behinds. So if there is a - at the start of the line it will be replaced by itself, for the other matches $1 is empty so replaced with nothing.