I have a feeling this is going to be a stupid question, but I'm completely baffled.
I'm using the XRegExp library for Javascript to generate a regular expression that matches any word in any alphabet, and does not match words with numbers, punctuation or special characters. The library is found here: http://xregexp.com/
This is what I have so far:
var tests = [
"Hello",
"Hello789",
"Hello£*&£",
"你好世界",
"你好世界((£&"
];
var reg = XRegExp('^\\p{L}+$');
for (var i = 0; i < tests.length; i++) {
console.log (tests[i] + " : " + (reg.test(tests[i]) ? "true" : "false"));
}
This produces the following output:
Hello : true
Hello789 : false
Hello£*&£ : false
你好世界 : true
你好世界((£& : false
I know that reg is matching the right thing, but how do I use it in a replace so that the numbers, punctuation and special characters are stripped out? The output I want is :
Hello : Hello
Hello789 : Hello
Hello£*&£ : Hello
你好世界 : 你好世界
你好世界((£& : 你好世界
These are the things I've tried so far, with no success:
XRegExp.replace(tests[i], '^\\p{L}+$', ''));
XRegExp.replace(tests[i], '\\p{L}+$', ''));
XRegExp.replace(tests[i], '^\\p{L}', ''));
XRegExp.replace(tests[i], '\\p{L}', ''));
All of these return the same string I gave them, with no changes at all. Any one have an idea what I'm doing wrong?
Your regular expression is matching only letters, but it sounds like you want to match everything except letters:
var reg = XRegExp('\\P{L}+', 'g');
Then you can replace everything that matches with the empty string.
XRegExp.replace(tests[i], reg, '');
Related
I want to have a regular expression in JavaScript which help me to validate a string with contains only lower case character and and this character -.
I use this expression:
var regex = /^[a-z][-\s\.]$/
It doesn't work. Any idea?
Just use
/^[a-z-]+$/
Explanation
^ : Match from beginning string.
[a-z-] : Match all character between a-z and -.
[] : Only characters within brackets are allowed.
a-z : Match all character between a-z. Eg: p,s,t.
- : Match only strip (-) character.
+ : The shorthand of {1,}. It's means match 1 or more.
$: Match until the end of the string.
Example
const regex= /^[a-z-]+$/
console.log(regex.test("abc")) // true
console.log(regex.test("aBcD")) // false
console.log(regex.test("a-c")) // true
Try this:
var regex = /^[-a-z]+$/;
var regex = /^[-a-z]+$/;
var strs = [
"a",
"aB",
"abcd",
"abcde-",
"-",
"-----",
"a-b-c",
"a-D-c",
" "
];
strs.forEach(str=>console.log(str, regex.test(str)));
Try this
/^[a-z-]*$/
it should match the letters a-z or - as many times as possible.
What you regex does is trying to match a-z one single time, followed by any of -, whitespace or dot one single time. Then expect the string to end.
Use this regular expression:
let regex = /^[a-z\-]+$/;
Then:
regex.test("abcd") // true
regex.test("ab-d") // true
regex.test("ab3d") // false
regex.test("") // false
PS: If you want to allow empty string "" to pass, use /^[a-z\-]*$/. Theres an * instead of + at the end. See Regex Cheat Sheet: https://www.rexegg.com/regex-quickstart.html
I hope this helps
var str = 'asdadWW--asd';
console.log(str.match(/[a-z]|\-/g));
This will work:
var regex = /^[a-z|\-|\s]+$/ //For this regex make use of the 'or' | operator
str = 'test- ';
str.match(regex); //["test- ", index: 0, input: "test- ", groups: undefined]
str = 'testT- ' // string now contains an Uppercase Letter so it shouldn't match anymore
str.match(regex) //null
I'm trying to extract out a group of words from a larger string/cookie that are separated by hyphens. I would like to replace the hyphens with a space and set to a variable. Javascript or jQuery.
As an example, the larger string has a name and value like this within it:
facility=34222%7CConner-Department-Store;
(notice the leading "C")
So first, I need to match()/find facility=34222%7CConner-Department-Store; with regex. Then break it down to "Conner Department Store"
var cookie = document.cookie;
var facilityValue = cookie.match( REGEX ); ??
var test = "store=874635%7Csomethingelse;facility=34222%7CConner-Department-Store;store=874635%7Csomethingelse;";
var test2 = test.replace(/^(.*)facility=([^;]+)(.*)$/, function(matchedString, match1, match2, match3){
return decodeURIComponent(match2);
});
console.log( test2 );
console.log( test2.split('|')[1].replace(/[-]/g, ' ') );
If I understood it correctly, you want to make a phrase by getting all the words between hyphens and disallowing two successive Uppercase letters in a word, so I'd prefer using Regex in that case.
This is a Regex solution, that works dynamically with any cookies in the same format and extract the wanted sentence from it:
var matches = str.match(/([A-Z][a-z]+)-?/g);
console.log(matches.map(function(m) {
return m.replace('-', '');
}).join(" "));
Demo:
var str = "facility=34222%7CConner-Department-Store;";
var matches = str.match(/([A-Z][a-z]+)-?/g);
console.log(matches.map(function(m) {
return m.replace('-', '');
}).join(" "));
Explanation:
Use this Regex (/([A-Z][a-z]+)-?/g to match the words between -.
Replace any - occurence in the matched words.
Then just join these matches array with white space.
Ok,
first, you should decode this string as follows:
var str = "facility=34222%7CConner-Department-Store;"
var decoded = decodeURIComponent(str);
// decoded = "facility=34222|Conner-Department-Store;"
Then you have multiple possibilities to split up this string.
The easiest way is to use substring()
var solution1 = decoded.substring(decoded.indexOf('|') + 1, decoded.length)
// solution1 = "Conner-Department-Store;"
solution1 = solution1.replace('-', ' ');
// solution1 = "Conner Department Store;"
As you can see, substring(arg1, arg2) returns the string, starting at index arg1 and ending at index arg2. See Full Documentation here
If you want to cut the last ; just set decoded.length - 1 as arg2 in the snippet above.
decoded.substring(decoded.indexOf('|') + 1, decoded.length - 1)
//returns "Conner-Department-Store"
or all above in just one line:
decoded.substring(decoded.indexOf('|') + 1, decoded.length - 1).replace('-', ' ')
If you want still to use a regular Expression to retrieve (perhaps more) data out of the string, you could use something similar to this snippet:
var solution2 = "";
var regEx= /([A-Za-z]*)=([0-9]*)\|(\S[^:\/?#\[\]\#\;\,']*)/;
if (regEx.test(decoded)) {
solution2 = decoded.match(regEx);
/* returns
[0:"facility=34222|Conner-Department-Store",
1:"facility",
2:"34222",
3:"Conner-Department-Store",
index:0,
input:"facility=34222|Conner-Department-Store;"
length:4] */
solution2 = solution2[3].replace('-', ' ');
// "Conner Department Store"
}
I have applied some rules for the regex to work, feel free to modify them according your needs.
facility can be any Word built with alphabetical characters lower and uppercase (no other chars) at any length
= needs to be the char =
34222 can be any number but no other characters
| needs to be the char |
Conner-Department-Store can be any characters except one of the following (reserved delimiters): :/?#[]#;,'
Hope this helps :)
edit: to find only the part
facility=34222%7CConner-Department-Store; just modify the regex to
match facility= instead of ([A-z]*)=:
/(facility)=([0-9]*)\|(\S[^:\/?#\[\]\#\;\,']*)/
You can use cookies.js, a mini framework from MDN (Mozilla Developer Network).
Simply include the cookies.js file in your application, and write:
docCookies.getItem("Connor Department Store");
in google script I am trying to replace a %string basing on the character following it.
I've tried using:
var indexOfPercent = newString.indexOf("%");
and then check the character of indexOfPercent+1, but indexOf returns only the first occurrence of '%'.
How can I get all occurrences? Maybe there is easier way to do that (regular expressions)?
EDIT:
Finally I want to replace all my % occurrences to %%, but not if percent sign was part of %# or %#.
To sum up: my string: Test%# Test2%s Test3%. should look like: Test%# Test2%s Test3%%.
I've tried using something like this:
//?!n Matches any string that is not followed by a specific string n
//(x|y) Find any of the alternatives specified
var newString = newString.replace(\%?![s]|\%?![%], "%%")
but it didn't find any strings. I am not familiar with regex's, so maybe it is a simple mistake.
Thanks
Try this code:
// replace all '%'
var StrPercent = '%100%ffff%';
var StrNoPersent = StrPercent.replace(/\%/g,'');
Logger.log(StrNoPersent); // 100ffff
Look for more info here
Edit
In your case you need RegEx with the character not followed by group of characters. Similiar question was asked here:
Regular expressions - how to match the character '<' not followed by ('a' or 'em' or 'strong')?
Thy this code:
function RegexNotFollowedBy() {
var sample = ['Test%#',
'Test2%s',
'Test3%',
'%Test4%'];
var RegEx = /%(?!s|#)/g;
var Replace = "%%";
var str, newStr;
for (var i = 0; i < sample.length; i++) {
str = sample[i];
newStr = str.replace(RegEx, Replace);
Logger.log(newStr);
}
}
I'll explain expression /%(?!s|#)/g:
% -- look '%'
(text1|text2|text3...|textN) -- not followed by text1, 2 etc.
g -- look for any accurance of searched text
I have this array that has emo symbols and associated image files for each emo path.
Working demo of my code on JSFIDDLE
But using this code, only :) this emo returns the correct smile.png image but rest of the emo are not working.
How do I write the correct regex that match each of these symbols and choose the appropriate file for each emo ?
//Replace Emo with images
function replaceEmoticons(text) {
var emoticons = {
':)' : 'smile.png',
': )' : 'smile.png',
':D' : 'laugh.png',
':->' : 'laugh.png',
':d' : 'laugh-sm.png',
':-)': 'smile-simple.png',
':p': 'tounge.png',
':P': 'tounge-lg.png',
': P': 'tng1.png',
'>-)': 'evil.png',
':(': 'sad.png',
':-(': 'sadd.png',
':-<': 'sadd.png',
':-O': 'surprise.png',
':O': 'sur2.png',
':o': 'sur3.png',
':-o': 'sur3.png',
':-*': 'kiss.png',
':*': 'kiss.png',
':-#': 'angry.png',
':#': 'angry.png',
':$': 'con2.png',
':-$': 'con1.png',
'O.o': 'con2.png',
'o.O': 'con2.png',
':/': 'weird.png',
':x': 'x.png',
':X': 'x.png',
':!': 's.png',
'(:I': 'egg.png',
'^.^': 'kit.png',
'^_^': 'kit.png',
';)': 'wink.png',
';-)': 'wink.png',
":')": 'hc.png',
":'(": 'cry.png',
"|-O": 'yawn.png',
"-_-": 'poke.png',
":|": 'p1.png',
"$_$": 'he.png'
}, url = "images/emo/";
// a simple regex to match the characters used in the emoticons
return text.replace(/[:\-)D]+/g, function (match) {
return typeof emoticons[match] != 'undefined' ?
'<img class="emo" src="'+url+emoticons[match]+'"/>' :
match;
});
}
replaceEmoticons("Hi this is a test string :) with all :P emos working :D");
This regex:
[:\-)D]+
Does not match many of the emoticons in your list. Any character other than :, \, -, ),or D will prevent it from being recognized.
If you have a list of strings you want to match, you can easily build a regex to match any of them (and nothing else) by escaping each one and joining them together with |. Something like this:
// given a string, return the source for a regular expression that will
// match only that exact string, by escaping any regex metacharacters
// contained within.
RegExp.escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
// build a regex that will match all the emoticons. Written out, it looks
// like this: /:\)|: \)|:D|:->|:d|:-\)|:p|:P|..../g
var emoticonRegex =
new RegExp(Object.keys(emoticons).map(RegExp.escape).join('|'), 'g');
Then use that in place of your literal regex:
return text.replace(emoticonRegex, function (match) { ...
I want to alter a text string with a regular expression, removing every non-digit character, except a + sign. But only if it's the first character
+23423424dfgdgf234 --> +23423424234
2344 234fsdf 9 --> 23442349
4+4 --> 44
etc
The replacing of 'everything but' is pretty simple:
/[^\+|\d]/gi but that also removes the +-sign as a first character.
how can I alter the regexp to get what I want?
If it matters: I'm using the regexp in javascript's str.replace() function.
I would do it in two steps, first removing everything that must be removed apart the +, then the + that aren't the first char :
var str2 = str1.replace(/[^\d\+]+/g,'').replace(/(.)\++/g,"$1")
You'd have to do this in two steps:
// pass one - remove all non-digits or plus
var p1 = str.replace(/[^\d+]+/g, '');
// remove plus if not first
var p2 = p1.length ? p1[0] + p1.substr(1).replace(/\+/g, '') : '';
console.log(p2);
You can replace the following regex
[^\d+] with ''
and then on the resulting string, replace
(.)\+ with '$1'
Demo: http://regex101.com/r/eT6uF6
Updated: http://jsfiddle.net/QVd7R/2/
You could combine the above suggested 2 replaces in a single RegExp:
var numberWithSign = /(^\+)|[^\d]+/g;
var tests =
[
{"input" : "+23423424dfgdgf234", "output" : "+23423424234"},
{"input" : "2344 234fsdf 9" , "output" : "23442349"},
{"input" : "4+4" , "output" : "44"},
{"input" : "+a+4" , "output" : "+4"},
{"input" : "+a+b" , "output" : "+"},
{"input" : "++12" , "output" : "+12"}
];
var result = true;
for (index in tests) {
var test = tests[index];
testResult = test.input.replace(numberWithSign,"$1");
result = result && (testResult == test.output);
if (!result) {
return testResult + "\n" + test.output;
}
}
return result;
Basically the first part (^\+) would match only the + sign in the beginning of the line, and will put it as $1, so when you replace this match with $1 it will keep the plus sign in the beginning of the string. If it does not match, then the next part of the regexp [^\d]+ will take effect, replacing all non-digits with an empty string (as there would be nothing in the value of $1)
Try this:
var newString = Yourstring.match(/(^\+)?\d*/g).join("");