0

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.

6
  • 2
    Welcome to StackOverflow! Have you tried anything so far? StackOverflow isn't a free code-writing service, and expects you to try to solve your own problem first. Please update your question to show what you have already tried, showing the specific problem you are facing in a minimal, complete, and verifiable example. For further information, please see how to ask a good question, and take the tour of the site Commented Oct 15, 2017 at 3:13
  • Try rotate[\d-]* Commented Oct 15, 2017 at 3:16
  • Than you Ibrahim for your quick reply, unfortunately I already tried that and it's not working :(. As requested by Jaromanda I will update my OP to put some examples of what I tried... Commented Oct 15, 2017 at 3:22
  • @Slander Try this \brotate[^\s]+ as shown HERE? Commented Oct 15, 2017 at 3:35
  • Woah! Nice website you pointed here! Wasn't aware of that one... Thanks a lot @Gunman! Sure your comment is more usefull than the one from Jaromanda... Commented Oct 15, 2017 at 3:40

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 ...............

Comments

1

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.