JavaScript string with regex search - javascript

I have a returned string consisting of multiple classes:
"xxx1sbu xxx2sdf xxx1sef xxx1sb1 xxx1su xxx1s1 dxxx1s xxx1sdfg xxx1sbf"
I need the regex to search the string and find the classes based on the following criteria:
1) class begins with 'xxx1s'
2) class is no longer than 7 characters (letters and numbers) excluding a following space ('xxx1sbu')
3) if a space follows the class ('xxx1sbu ') then it is also found so that it can be removed.
I then use the regex to replace the found classes to
var classesReplaced = classesString.replace(regex, "")
The string should then look as follows:
"xxx2sdf dxxx1s xxx1sdfg"
So far the best I have come up with is:
RegExp: /\bxxx1s([a-z1-9])([a-z1-9])(\s)|\bxxx1s([a-z1-9])([a-z1-9])\b/g
pattern: \bxxx1s([a-z1-9])([a-z1-9])(\s)|\bxxx1s([a-z1-9])([a-z1-9])\b
I also tried to use javascript to build the expression but it keeps stripping off the '\b':
var classId = 'xxx1s';
var regex2 = new RegExp('\b'+ classId +'([a-z1-9])([a-z1-9])(\s)|\b'+ classId +'([a-z1-9])([a-z1-9])\b','g');
Is there a better way to write this?? My understanding of regex isn't great!
Thank you in advance

I think this is the command and regex you need:
NewString = OldString.replace( /\bxxx1s\S{0,2}\b\s?/, "" );
Here's how that breaks down:
\b is the opening word border for the section you want to replace.
xxx1s is the base text you want to remove.
\S{0,2} (upper-case S) finds any non-white-space between 0 and 2 characters long; added to the item above, it makes sure that only items of 7 or fewer characters are found.
\b is the closing word border.
\s? (lower-case s) finds the trailing space; the ? makes it optional so the last item in the series won't be skipped.

Here is the regex you should use:
var regex = /((?:\s*(xxx1s)\w{1,2})(?=\W))|(xxx1sbu )/g;

Related

regex javascript locale - replacing word boundery with a suitable character before and after [duplicate]

So I'm completely new to regular expressions, and I'm trying to use Java's java.util.regex to find punctuation in input strings. I won't know what kind of punctuation I might get ahead of time, except that (1) !, ?, ., ... are all valid puncutation, and (2) "<" and ">" mean something special, and don't count as punctuation.
The program itself builds phrases pseudo-randomly, and I want to strip off the punctuation at the end of a sentence before it goes through the random process.
I can match entire words with any punctuation, but the matcher just gives me indexes for that word. In other words:
Pattern p = Pattern.compile("(.*\\!)*?");
Matcher m = p.matcher([some input string]);
will grab any words with a "!" on the end. For example:
String inputString = "It is a warm Summer day!";
Pattern p = Pattern.compile("(.*\\!)*?");
Matcher m = p.matcher(inputString);
String match = inputString.substring(m.start(), m.end());
results in --> String match ~ "day!"
But I want to have Matcher index just the "!", so I can just split it off.
I could probably make cases, and use String.substring(...) for each kind of punctuation I might get, but I'm hoping there's some mistake in my use of regular expressions to do this.
Java does support POSIX character classes in a roundabout way. For punctuation, the Java equivalent of [:punct:] is \p{Punct}.
Please see the following link for details.
Here is a concrete, working example that uses the expression in the comments
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexFindPunctuation {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\p{Punct}");
Matcher m = p.matcher("One day! when I was walking. I found your pants? just kidding...");
int count = 0;
while (m.find()) {
count++;
System.out.println("\nMatch number: " + count);
System.out.println("start() : " + m.start());
System.out.println("end() : " + m.end());
System.out.println("group() : " + m.group());
}
}
}
I would try a character class regex similar to
"[.!?\\-]"
Add whatever characters you wish to match inside the []s. Be careful to escape any characters that might have a special meaning to the regex parser.
You then have to iterate through the matches by using Matcher.find() until it returns false.
I would try
\W
it matches any non-word character. This includes spaces and punctuation, but not underscores. It’s equivalent to [^A-Za-z0-9_]
I was tring to find how to replace a regex, with keeping other regex part.
Example: Hi , how are you ? -> Hi, how are you?.
After studying a little i found that i could create groups, using "()", so just replaced the goup one, that was "(\s)".
String a = "Hi , how are you ?";
String p = "(\s)([,.!?\\-])";
System.out.println(a.replaceAll(p,"$2"));
//output: Hi, how are you?

I need some help for a specific regex in javascript

I try to set a correct regex in my javascript code, but I'm a bit confused with this. My goal is to find any occurence of "rotate" in a string. This should be simple, but in fact I'm lost as my "rotate" can have multiple endings! Here are some examples of what I want to find with the regex:
rotate5
rotate180
rotate-1
rotate-270
The "rotate" word can be at the begining of my string or at the end, or even in the middle separated by spaces from other words. The regex will be used in a search-and-replace function.
Can someone help me please?
EDIT: What I tried so far (probably missing some of them):
/\wrotate.*/
/rotate.\w*/
/rotate.\d/
/\Srotate*/
I'm not fully understanding the regex mechanic yet.
Try this regex as a start. It will return all occurrences of a "rotate" string where a number (positive or negative) follows the "rotate".
/(rotate)([-]?[0-9]*)/g
Here is sample code
var aString = ["rotate5","rotate180","rotate-1","some text rotate-270 rotate-1 more text rotate180"];
for (var x = 0; x < 4; x++){
var match;
var regex = /(rotate)([-]?[0-9]*)/g;
while (match = regex.exec(aString[x])){
console.log(match);
}
}
In this example,
match[0] gives the whole match (e.g. rotate5)
match[1] gives the text "rotate"
match[2] gives the numerical text immediately after the word "rotate"
If there are multiple rotate stings in the string, this will return them all
If you just need to know if the 'word' is in the string so /rotate/ simply will be OK.
But if you want some matching about what coming before or after the #mseifert will be good
If you just want to replace the word rotate by another one
you can just use the string method String.replace use it like var str = "i am rotating with rotate-90"; str.repalace('rotate','turning')'
WHy your regex doesnt work ?
/\wrotate.*/
means that the string must start with a caracter [a-zA-Z0-9_] followed by rotate and another optional character
/rotate.\w*/
meanse rotate must be followed by a character and others n optional character
...............
Using your description:
The "rotate" word can be at the beginning of my string or at the end, or even in the middle separated by spaces from other words. The regex will be used in a search-and-replace function.
This regex should do the work:
const regex = /(^rotate|rotate$|\ {1}rotate\ {1})/gm;
You can learn more about regular expressions with these sites:
http://www.regular-expressions.info
regex101.com and btw here is an example using your requirements.

Regex trying to match characters before and after symbol

I'm trying to match characters before and after a symbol, in a string.
string: budgets-closed
To match the characters before the sign -, I do: ^[a-z]+
And to match the other characters, I try: \-(\w+) but, the problem is that my result is: -closed instead of closed.
Any ideas, how to fix it?
Update
This is the piece of code, where I was trying to apply the regex http://jsfiddle.net/trDFh/1/
I repeat: It's not that I don't want to use split; it's just I was really curious, and wanted to see, how can it be done the regex way. Hacking into things spirit
Update2
Well, using substring is a solution as well: http://jsfiddle.net/trDFh/2/ and is the one I chosed to use, since the if in question, is actually an else if in a more complex if syntax, and the chosen solutions seems to be the most fitted for now.
Use exec():
var result=/([^-]+)-([^-]+)/.exec(string);
result is an array, with result[1] being the first captured string and result[2] being the second captured string.
Live demo: http://jsfiddle.net/Pqntk/
I think you'll have to match that. You can use grouping to get what you need, though.
var str = 'budgets-closed';
var matches = str.match( /([a-z]+)-([a-z]+)/ );
var before = matches[1];
var after = matches[2];
For that specific string, you could also use
var str = 'budgets-closed';
var before = str.match( /^\b[a-z]+/ )[0];
var after = str.match( /\b[a-z]+$/ )[0];
I'm sure there are better ways, but the above methods do work.
If the symbol is specifically -, then this should work:
\b([^-]+)-([^-]+)\b
You match a boundry, any "not -" characters, a - and then more "not -" characters until the next word boundry.
Also, there is no need to escape a hyphen, it only holds special properties when between two other characters inside a character class.
edit: And here is a jsfiddle that demonstrates it does work.

Javascript regex: test if text contains only characters from a set

How it is better to check (test) if text contains only characters from set (for example if text contains only punctuation marks)
var regex = /[\.,-\/#!$%\^&\*;:{}=\-_`~()]/g
res = text.replace(regex, '')
if (res) return false
so I made it with replace is it possible to do it with regex.test?
Yes it is. There are two possibilities. One is, that you use anchors to assert that the full string is made up of these:
var regex = /^[\.,-\/#!$%\^&\*;:{}=\-_`~()]+$/;
if(regex.test(text))
Alternatively you can use a negated character class and see whether it matches and then again negate the result
var regex = /[^\.,-\/#!$%\^&\*;:{}=\-_`~()]/;
if(!regex.test(text))
Note that ,-\/ is a range that includes ,-./. This is redundant and may become a source of errors if the character class is ever changed. You might want to simplify your character class to:
[.,\/#!$%^&*;:{}=_`~()-]
(Or the negated version of that, depending on which approach you choose.)

Split string in JavaScript using a regular expression

I'm trying to write a regex for use in javascript.
var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var match = new RegExp("'[^']*(\\.[^']*)*'").exec(script);
I would like split to contain two elements:
match[0] == "'areaog_og_group_og_consumedservice'";
match[1] == "'\x26roleOrd\x3d1'";
This regex matches correctly when testing it at gskinner.com/RegExr/ but it does not work in my Javascript. This issue can be replicated by testing ir here http://www.regextester.com/.
I need the solution to work with Internet Explorer 6 and above.
Can any regex guru's help?
Judging by your regex, it looks like you're trying to match a single-quoted string that may contain escaped quotes. The correct form of that regex is:
'[^'\\]*(?:\\.[^'\\]*)*'
(If you don't need to allow for escaped quotes, /'[^']*'/ is all you need.) You also have to set the g flag if you want to get both strings. Here's the regex in its regex-literal form:
/'[^'\\]*(?:\\.[^'\\]*)*'/g
If you use the RegExp constructor instead of a regex literal, you have to double-escape the backslashes: once for the string literal and once for the regex. You also have to pass the flags (g, i, m) as a separate parameter:
var rgx = new RegExp("'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'", "g");
while (result = rgx.exec(script))
print(result[0]);
The regex you're looking for is .*?('[^']*')\s*,\s*('[^']*'). The catch here is that, as usual, match[0] is the entire matched text (this is very normal) so it's not particularly useful to you. match[1] and match[2] are the two matches you're looking for.
var script = "function onclick() {loadArea('areaog_og_group_og_consumedservice', '\x26roleOrd\x3d1');}";
var parameters = /.*?('[^']*')\s*,\s*('[^']*')/.exec(script);
alert("you've done: loadArea("+parameters[1]+", "+parameters[2]+");");
The only issue I have with this is that it's somewhat inflexible. You might want to spend a little time to match function calls with 2 or 3 parameters?
EDIT
In response to you're request, here is the regex to match 1,2,3,...,n parameters. If you notice, I used a non-capturing group (the (?: ) part) to find many instances of the comma followed by the second parameter.
/.*?('[^']*')(?:\s*,\s*('[^']*'))*/
Maybe this:
'([^']*)'\s*,\s*'([^']*)'

Categories