2

I am creating an aplication in javaScript, but I am a bit confused of RegExp. I need to make user type into textArea desired form of string and let aplication to work with it, so I need to restrict the textArea input.

I need to field be like this: 760, 8,20, 50/3, 10,160/40, 3001

with optional spaces after comma, and optional number of items, where some of them can be optionaly with sufix (*/1...)

also i would like to have similar pattern which behave simillary but there could be prefixes like this:

st. 760,st. 8, st.20, st. 50/3, st. 10, st. 160/40, st. 3001

also with optional spaces. I would be really happy if someone could help me with making this regExp, because I am a real begginer in creating this...

I've tried the following regex ((\d+)|((\d+)/(\d+))),(\s?).

Thanks in advance!

3
  • 1
    You want a lot, what have you tried ? Commented Jul 29, 2013 at 9:46
  • i tried something like this: ((\d+)|((\d+)/(\d+))),(\s?) but i think it doesn't cover whole problem... Commented Jul 29, 2013 at 9:52
  • @user2567998 Well you can always test your regex at regexpal. Commented Jul 29, 2013 at 9:58

3 Answers 3

1

I would use something like this:

^\d+(/\d+)?(\s*,\s*\d+(/\d+)?)*$

Then, for the second one:

^(st\.\s*)?\d+(/\d+)?(\s*,\s*(st\.\s*)?\d+(/\d+)?)*$

Remember, any time you can use the escape sequences, which will make your regex much clearer. So, instead of doing [0-9], use \d, and also use \s to accept all kinds of spaces (space, tab; etc).

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

1 Comment

this one is great! i slightly edited it: /^(st\.\s*)?\d+(\/\d+)?(\s*,\s*(st\.\s*)?\d+(\/\d+)?)*$/im but it is exacly what I needed! thanks for taking your time and helping me!
1

I think

^([ ]*[0-9]+(/[0-9]+)?[ ]*\,)*([ ]*[0-9]+(/[0-9]+)?[ ]*)$

and

^([ ]*(st\. )?[0-9]+(/[0-9]+)?[ ]*\,)*([ ]*(st\. )?[0-9]+(/[0-9]+)?[ ]*)$

sound about right

3 Comments

it looks pretty great, however when i type something like this: st. 760, gfn, st. 8 it also accepts this one, but i need to prevent to user write other symbols than digits (exept "st.")
Corrected, need to add ^ and $
this is very nice. Thank you very much for making your time and helping me! I will prefer cgledezma's pattern because it has oportunity to use \n new line and it is shorter but they are similar, thank you once more. I really apreciate your help!
0

I hope it could help you:

To select your field:

    ([0-9]+/[0-9]+[,]{0,1})|([0-9]+[,]{0,1}) 

To replace them:

    st\.\ &1&2 

By using sed command line :

    sed -e "s/([0-9]+/[0-9]+[,]{0,1})|([0-9]+[,]{0,1})/st\.\ &1&2/g"

1 Comment

i don't need to have my string replaced and to have "st. " inserted before each number. I only need to have checked the textArea if it matches the regExp

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.