This question already has answers here:
Count the number of occurrences of a character in a string in Javascript
(39 answers)
Closed 4 years ago.
I'm trying to create a Giveaway Picker for Instagram. The Rules for the Giveaway are: tag 3 Friends in the comments.
A regular comment should now be #pers1 #pers2 #pers3. How can I check if there are at least 3 # symbols in the comment.
I alredy loaded all comments into the variable comments.
I think it would work with Regex. But I'm not so good in regex. Does anyone have a working regex for this? THX.
If you want to use a regular expression, you can use a global match and test the length of the result:
const numberOfAts = str => str.match(/#/g).length;
console.log(numberOfAts('#pers1 #pers2 #pers3'));
console.log(numberOfAts('#pers1 #pers2 #pers3 #pers4'));
console.log(numberOfAts('#pers1 #pers2'));
Related
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
I am looking for a JavaScript RegEx that matches all woff and woff2 files that start with a particular String, i.e. MyFont.
The following examples should match:
MyFont-bold.woff
MyFont-light.woff2
The following example should not match:
GoogleFont-bold.woff
SomeOtherFont-light.woff2
MyFont-something.css
^MyFont.*.woff2?$/ would work.
var regex = /^MyFont.*.woff2?$/
console.log(regex.test('MyFont-bold.woff'))
console.log(regex.test('MyFont-light.woff2'))
console.log(regex.test('GoogleFont-bold.woff'))
console.log(regex.test('SomeOtherFont-light.woff2'))
console.log(regex.test('MyFont-something.css'))
This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I am trying to get the text between the two tags which also handles multiple lines.I have managed to do it in PHP regex but i am stuck on the javascript one.
<any_tag>random text
dffdffdfdfdfdfdfdfd
dfdfdfdfdfdfdfdfdf
</any_tag>
I want to get the text between <any_tag> and </any_tag>
This is the regex for php
https://regex101.com/r/tQ1bX7/2
Now i am trying to achieve to same in javascript
Give try on following :
/[^(<any_tag>)](?:.*(\r?\n?).*)*(?=\<\/any_tag\>)/gi
Explination :
[^(<any_tag>)] match a single character not present in the list below
(<any_tag>) a single character in the list (<any_tg>) literally (case insensitive)
This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 7 years ago.
An API I use returns this text:
<http://192.168.1.10:8080/longUrl>; rel="recording-session",
<http://192.168.1.10:8080/realLongUrl>; rel="h264-session-sdp",
<http://192.168.1.10:8080/realLongDifferentUrl>; rel="h264-session-sdp",
<rtp://239.1.1.18:5006>; rel="destination-high",
<rtp://239.1.1.17:5006>; rel="destination-low"
I'm trying to retrieve the first URL that is followed by ; rel="h264-session-sdp.
So in this case that would be: http://192.168.1.10:8080/realLongUrl
I've been fiddling around trying to modify examples found here on SO, but just can's seem to get it right.
try this one /([^<]+)(?:>; rel\=\"h264\-session\-sdp\")/
Selects the text inbetween the greater then and less then characters:
?<=\<)(.*?)(?=>)
https://regex101.com/r/oS5sX6/1
And if you wanted to select the urls on multiple lines, add the g and m modifiers:
/(?<=\<)(.*?)(?=>)/gm
https://regex101.com/r/oS5sX6/2
Try this:
/(?<=\<)(.*?)(?=\>)>; rel="h264-session-sdp"/
This question already has answers here:
How can I replace a string in parentheses using a regex?
(4 answers)
Closed 7 years ago.
I need to replace the text between two parentheses using Regex in Javascript. For example:
var x = "I need to go (now)";
I need to replace 'now' with 'tomorrow'. I tried this, but it didn't work:
x.replace(/\(now)\b/g, 'tomorrow');
"I need to know (now)".replace(/\(now\)/g, 'tomorrow');
You don't need the \b and you need to escape the second ).
This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 7 years ago.
I am trying to replace the same string *a*a consistently with *a.
Tried many variations of something like this, but none really worked:
s = s.replace( /\b*a*a\b/g, "*a");
So far running this leads to all xzy*a being replaced with xyz
* is a special regex character. If you want to match only an actual asterisk, then you have to escape it like this:
s = s.replace( /\*a\*a/g, "*a");
Working demo: http://jsfiddle.net/jfriend00/gvgshwyz/
An asterisk is a special regex character.
You just have to escape it like this: \*a in place of *a