I'm in the process of porting some php code I have to nodejs.
The issue I have concerns this PCRE regex:
/\/?_?[0-9]*_?([^\/\._]*)[_#*\-*\.?\p{L}\p{M}*]*$/u
(this regex matches first
in _4_first_ääää
,in _first_äääää
or first_äääää
)
I'm using XRegExp in this context, but with no luck:
// lib/parser.js
var XRegExp = require('xregexp').XRegExp;
module.exports = {
getName : function(string){
var name = XRegExp('\/?_?[0-9]*_?([^\/\._]*)[_#*\-*\.?\p{L}\p{M}*]*$');
var matches = XRegExp.exec(string, name);
if(matches && matches.length > 0){
return matches[1];
}
else{
return '';
}
}
};
And the test (mocha) that goes with it:
// test/test.js
var assert = require("assert");
var parser = require('../lib/parser.js');
describe('parser', function(){
describe('#getName()', function(){
it('should return the name contained in the string', function(){
assert.equal('test', parser.getName('3_test'));
assert.equal('test', parser.getName('test'));
assert.equal('test', parser.getName('_3_test'));
assert.equal('test', parser.getName('_3_test_ääää'));
assert.equal('test', parser.getName('_3_test_boom'));
})
})
})
And the tests results:
0 passing (5ms)
1 failing
1) parser #getName() should return the name contained in the string:
AssertionError: "test" == "ääää"
+ expected - actual
+ääää
-test
This code matches ääää
.
The commented line catches first
so I guess I'm missusing the unicodes caracter classes.
My question is: how can I make my original php regex work in javascript?
Mmaybe there is a work around?