How do I properly use RegExp in JavaScript and PHP? [duplicate] - javascript

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How do I properly use RegExp?
var text = "here come dat boi o shit waddup";
var exmaple = /[a-zA-Z0-9 ]/; // allowes a-zA-Z0-9 and whitespaces but nothing else right?
example.test(test); // would return true right?
text = "%coconut$§=";
example.test(text); // would return false right?
//I know this is very basic - I started learnig all this about week ago
Are JS RegExp's the same as PHP RegExp's?
How do I define banned characters instead of defining allowed characters?
How do I make it so that the var text has to contain 3 (or more) numbers/letters?
How do I include / or ",'$ etc. in my pattern?

No.
Use ^ character (i.e. [^abc] will exclude a, b and c)
Use [A-Za-z]{3} for letters and \d{3} for digits. If you want 3 or more, use \d{3,}
Use escape character (\/, \', \", '\$')

Related

Regex pattern for //;\n2;3;4 [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 1 year ago.
I am trying to find a regex which supports a pattern like below:
String starts with //
String has a delimiter after //; (suppose ; is the delimiter)
String has \n after demiliter (//;\n)
Finally String contains any number of digits with that delimiter (//;\n2;3;4;5)
Could you help?
I tried ^//\\D+\\n.*$ but it doesn't work.
Thanks in advance!
Sample: //;\n2;3;4;5
Answer: [/]{2}[;]\\[n](\d[;]){1,999}\d
This will allow further combinations of a decimal followed by ;
\d is added at the end in the case a semicolon is not added judging by your sample
Okay based on your additional comment this could work. It's very messy but it may just get the job done.
var string = "//;\n2;3;4;5";
console.log(
string.replace(/[^0-9,.]+/g," ").trim().split(" ").map(function(x){return parseInt(x, 10);}).reduce(function(a, b){return a + b;}, 0)
);
Console log results in 14

How do remove all Unicode from string, BUT keep lanauges such as: Japanese, Greek, Hindi etc [duplicate]

This question already has answers here:
How can I use Unicode-aware regular expressions in JavaScript?
(11 answers)
Closed 4 years ago.
How would I remove all Unicode from this string【Hello!】★ ああああ
I need to remove all the "weird" symbols (【, ★, 】) and keep "Hello!" and "ああああ". This needs to work for all languages not just Japanese.
You want to remove characters within the Unicode categories Other Symbol, Combining Symbol, and Enclosing Mark, but leave those from other categories.
Using regular expressions, those match the classes \p{So}, \p{Sk} and \p{Me}, respectively. You might for example use XRegExp.replace().
I have found a solution. Using XRegEXP, I was able to use PHP's \p{Common} in node.
const xreg = require('xregexp');
let str = '【Hello!】★ ああああ】';
let regex = new xreg('\\p{Common}', 'g');
let res = xreg.replace(str, regex, ' ');
console.log(res); // Hello ああああ

Regex gives different result in javascript and .NET [duplicate]

This question already has an answer here:
Convert JavaScript Regex to C#
(1 answer)
Closed 4 years ago.
I need a regex which matches pattern name1\name2 with the restriction that name1 must not contain some special characters such as < , >. name1, name2 can have spaces.
I am using this regex and it seems to work fine in java script :
/^[^ &<>;]+\\./
In my C sharp code, I am using the regex below:
var pattern= #"^[^ &<>;]+\\.";
The C sharp results fail for the input : 8 [ } \ ;
where as it passes for javascript.
How can I get similar results ?
Problem with the example that you've given, you ommited space from list of allowed characters. For your example this pattern works:
var pattern = #"^[^&<>;]+\\.";

Javascript - Make specific text a link [duplicate]

This question already has answers here:
Detect URLs in text with JavaScript
(15 answers)
Closed 4 years ago.
I'm looking at a 'to do' list application that uses JavaScript.
One of the functions converts text in to a hyperlink when https, https or ftp is present.
I'd like to expand this so if my text contains # followed by any 5 digit number, that becomes a link as well. The URL should be http://192.168.0.1/localsite/testschool/search.php?id=ID
where ID is the # and 5 digit number.
This is the current Javascript:
function prepareHtml(s)
{
// make URLs clickable
s = s.replace(/(^|\s|>)(www\.([\w\#$%&~\/.\-\+;:=,\?\[\]#]+?))(,|\.|:|)?(?=\s|"|<|>|\"|<|>|$)/gi, '$1$2$4');
return s.replace(/(^|\s|>)((?:http|https|ftp):\/\/([\w\#$%&~\/.\-\+;:=,\?\[\]#]+?))(,|\.|:|)?(?=\s|"|<|>|\"|<|>|$)/ig, '$1$2$4');
};
called using prepareHtml(item.title)
Any idea how I can do this ?
I've worked out regex to match the # and 5 digits is ^#([0-9]{5}) but I'm not sure how to implement this in the function.
Thanks
Looks to me that the pattern & replace string can be simplified a bit.
function urls2links(s)
{
return s.replace(/(^|[\s>])(www\.)/gi, '$1http://$2')
.replace(/\b(((https?|ftps?):\/{2})(\S+?))(?="|<|>|[\s<>\"]|$)/ig, '$1');
};
var str = 'blah http://192.168.0.1/localsite/testschool/search.php?id=#12345 \nblah www.foo.com/bar/" blah';
console.log('--\n-- BEFORE\n--');
console.log(str);
console.log('--\n-- AFTER\n--');
console.log(urls2links(str));
Although I'm not too sure about needing to include the character entities |"|<|> in the lookahead.
But I'm guessing you also deal with encoded strings.

Detecting POboxes (form validation) [duplicate]

This question already has answers here:
Jquery PO BOX validation
(2 answers)
PO Box Regular Expression Validation
(15 answers)
Closed 9 years ago.
I have a function from another thread that helps to detect POBoxes, but it doesn't quite work as intended.
function isPOBox(v){
var r = new RegExp('[PDO.]*\\s?B(ox)?.*\\d+', 'i');
return v.match(r);
}
If I have the value 'Lvl 1 TowerB, 999 C G Road' it incorrectly picks it up as a PObox.
As you can see, there's no P in the above.
How would I go about editing the regex to be more specific around POBoxes?
I have set up a demo Fiddle here: http://jsfiddle.net/xCQwM/
If you look at the actual match:
> "Lvl 1 TowerB, 999 C G Road".match(new RegExp('[PDO.]*\\s?B(ox)?.*\\d+',"i"))
[ 'B, 999',
undefined,
index: 11,
input: 'Lvl 1 TowerB, 999 C G Road' ]
That is a match because:
[PDO.]\* indicates that the first part of the match is optional
\\s? is optional
(ox)? is optional
.* is optional
One set of strings that will match your regex is:
"B" followed by any number of characters followed by a digit
In your example, the match looks like
"B" matches "B"
"," matches ".*"
"999" matches "\\d+"
You need to give more details regarding what you expect a P.O. Box to look like in order for us to give a better regex
The answer to your question, as you have currently worded it, is to replace [PDO.]* with [PDO.]+ so it matches at least one char. You might want to use ([PDO]\\.){2} though.
I'm thinking something like this might be better:
([PDO]\\.){2}\\s?B(ox|\\.)\\s?\\d+

Categories

Resources