Inverse regex javascript - javascript

I'm trying to remove everything in a string that does not match 'standard' characters. Heres what I have so far:
var result = myString.replace(/^(?![A-Za-z0-9]+)/g, '');
Which does not work. Can someone point to me what I'm not doing right?

I think you mean this:
var result = myString.replace(/[^a-z0-9]/gi,'');

Related

how to make a regexp in js split to break using special text characters

I need help to make this work in JS:
a="casa-me,pois estou farto! Eis a lista:uma;duas;três."
a.split(/regex/) to return
a=["casa","me","pois","estou","farto","Eis","a","lista","uma","duas","três"]
Thank you.
Other answers seem to miss things like accented characters, etc. This seems to be working well against your test string, though:
var re = /[^A-zÀ-ÿ]+/g;
var str = 'casa-me,pois estou farto! Eis a lista:uma;duas;três';
var result = str.split(re);
alert(result);
here you go:
a.match(/\w+/g).join(' ');
What you want is a global matching regex (note the g modifier):
yourString.replace(/[,;:.!]/g, ' ')
Regexp is something very nice.
var a = 'casa-me,pois estou farto! Eis a lista:uma;duas;três.';
var array = a.match(/[àáâãèéêẽìíîĩòóôõùúûũ\w]+/g);
Each array position will have a word as you want.

Match #(\w+) and replace in javascript

I'm trying to match #(\w+) in a div content and remove it.
Here's what i've tried : http://jsfiddle.net/mxgde6m7/1/ .
#(\w+) works , but it doesn't replace with space.
var content = document.getElementById('contentbox');
var find = '#(\w+)';
var reg = new RegExp(find, 'g');
var result = content.innerHTML.replace(reg, ' ');
alert(result);
<div id="contentbox">#d test
What i want: <div id="contentbox">test
</div>
Thanks in advance.
EDIT
Okay, one problem solved, another one came up.
My script http://jsfiddle.net/mxgde6m7/9/ works perfectly there, but when i try it on my website, only a half works. The last part where it should replace #(\w+) with space doesn't work at all. If i copy/paste the CONTENT of the function in console(chrome), it works , but if i paste the function and i call it, it doesn't work.
Please help ! I'm stuck.
Using a RegExp constructor, you need two backslashes \\ in place of each backslash \.
var find = '#(\\w+)';
hwnd is correct that you need to double escape \w in your regular expression.
var find = '#(\\w+)';
But, you could also make this code much cleaner by defining a regex literal like so -
var content = document.getElementById('contentbox');
var result = content.innerHTML.replace(/#(\w+)/g, ' ');
alert(result);
Doing it this way doesn't require double escaping, as it's not a string.

Javascript String Split

I've a String like following:
var str = '35,35,105,105,130,208,50,250';
I would like to split this string and get like this:
var arr = [[35,35],[105,105],[130,208],[50,250]];
I tried some ways, but none of them give me anything. I tried with looping to find the even comma position and split, but that doesn't seem good to me. Please give me some suggestion on this. I'm looking for RegEx Solution.
One possible approach:
'35,35,105,105,130,208,50,250'.match(/\d+,\d+/g).map(function(s) {
return s.split(',');
});
Another crazy idea in one line:
JSON.parse('['+ '35,35,105,105,130,208,50,250'.replace(/\d+,\d+/g, '[$&]') +']');
Here's one way to do it.
var values = str.split(','),
output = [];
while(values.length)
output.push(values.splice(0,2));
If str contains an odd number of values, the last array in output will contain only one value using this method.

Get portion of a string using Javascript

I have a string (from the pathname in the url) and I am trying to pull out part of it, but I'm having trouble.
This is what I have so far:
^(/svc_2/pub/(.*?).php)
The string is:
/svc_2/pub/stats/dashboard.php?ajax=1
How can I get a regex that returns /pub/stats/dashboard only?
If it's always that format (I'm assuming the /svc_2/ is always there) this should do it.
var s = "/svc_2/pub/stats/dashboard.php?ajax=1";
var match = s.match(/\/svc_2(.+)\./)[1];
But not if anything comes before that.
For this, using a regex is too much, but here is:
var string = "/svc_2/pub/stats/dashboard.php?ajax=1";
console.log(string.replace(/.*(\/pub.*)\.php.*$/,"$1"));
But you can do it, without a regex, like this
console.log(string.substr(6,string.indexOf(".php")-6));
In both cases, the console.log will give you /pub/stats/dashboard
This is not very flexible, but should work in this specific case.
var basedir = '/svc_2/', str = '/svc_2/pub/stats/dashboard.php?ajax=1';
str = str.substring(basedir.length, str.indexOf('.'));
alert(str);

how to extract this kind of data and put them into a nice array?

I got a string like this one:
var tweet ="#fadil good:))RT #finnyajja: what a nice day RT #fadielfirsta: how are you? #finnyajja yay";
what kind of code should work to extract any words with # character and also removing any special char at the end of the words? so it would an array like this :
(#fadil, #finnyajja, #fadielfirsta, #finnyajja);
i have tried the following code :
var users = $.grep(tweet.split(" "), function(a){return /^#/.test(a)});
it returns this:
(#fadil, #finnyajja:, #fadielfirsta:, #finnyajja)
there's still colon ':' character at the end of some words. What should I do? any solution guys? Thanks
Here is code that is more straightforward than trying to use split:
var tweet_text ="#fadil good:))RT #finnyajja: what a nice day RT #fadielfirsta: how are you? #finnyajja yay";
var result = tweet_text.match(/#\w+/g);
The easiest way without changing your current code too much would be to just remove all colons prior to calling split:
var users = $.grep(tweet_text.replace(":","").split(" "), function(a){return /^#/.test(a)});
You could also write a regex to do all the work for you using match. Something like this:
var regex = /#[a-z0-9]+/gi;
var matches = tweet.match(regex);
This assumes that you only want letters and numbers, if certain other characters are allowed, this regex will need to be modified.
http://jsfiddle.net/YHM87/

Categories

Resources