正则表达式:匹配两个条件之一?

时间:2016-09-21 09:49:19

标签: javascript angularjs regex angularjs-ng-pattern

嗨,我对常规表达不太了解。但我需要使用angularJs进行表单验证。

以下是要求

输入框只有在

的情况下才能接受

(1)前2个字母alpha + 6数字

(2)8数字

以下是一些正确的输入: -

(1)SH123456 (2)12345678 (3)sd456565

我试过 public class BuilderPage extends ActionBarActivity implements RecyclerViewAdapter.OnItemClickListener { private RecyclerView myRecyclerView; private LinearLayoutManager linearLayoutManager; private RecyclerViewAdapter myRecyclerViewAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.builder_layout); ButterKnife.bind(this); setSupportActionBar(toolbar1); getSupportActionBar().setDisplayShowTitleEnabled(false); dbHelper = new DbHelper(this); dbHelper.getWritableDatabase(); ArrayList<String> nameList = getIntent().getStringArrayListExtra("key"); myRecyclerView = (RecyclerView)findViewById(R.id.recyclerView_builderxml); linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false); myRecyclerViewAdapter = new RecyclerViewAdapter(this,nameList); myRecyclerView.setAdapter(myRecyclerViewAdapter); myRecyclerView.setLayoutManager(linearLayoutManager); myRecyclerViewAdapter.setOnItemClickListener(this); slidingDrawer.open(); slidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { @Override public void onDrawerClosed() { slideHandleButton.setImageDrawable(getResources().getDrawable(R.drawable.arrowup)); } }); slidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { @Override public void onDrawerOpened() { slideHandleButton.setImageDrawable(getResources().getDrawable(R.drawable.arrowdown)); } }); } @OnClick(R.id.imgviewtextbox) public void OnClick_TextBox(View view) { Intent intent = new Intent(getApplicationContext(),MyEditor.class); intent.putExtra("item","textbox"); startActivity(intent); } @OnClick(R.id.imgviewplaintext) public void OnClick_PlainText(View view) { Intent intent = new Intent(getApplicationContext(),MyEditor.class); intent.putExtra("item","plaintext"); startActivity(intent); } @OnClick(R.id.imgviewcheckbox) public void OnClick_CheckBox(View view) { dbHelper.Insert_RadioButton("CheckBox"); Intent intent = new Intent(getApplicationContext(),MyEditor.class); intent.putExtra("item","checkbox"); startActivity(intent); } @OnClick(R.id.imgviewradiobutton) public void OnClick_RadioButton(View view) { dbHelper.Insert_RadioButton("RadioButton"); Intent intent = new Intent(getApplicationContext(),MyEditor.class); intent.putExtra("item","radiobutton"); startActivity(intent); } ,它对上述两种情况都很好但仍然接受像S2D3E4F5这样的字符串,也可能是很多其他组合。

我做错了什么我无法找到它。

任何帮助都很明显!!!

谢谢

2 个答案:

答案 0 :(得分:2)

在你的正则表达式中,两个替代分支分别锚定:

  • (^([a-zA-Z]){2}([0-9]){6}) - 字符串开头的2个字母和6位数字
  • | - 或
  • ([0-9]*)?$ - 字符串末尾的可选零个或多个数字

您需要调整组的边界:

data-ng-pattern="/^([a-zA-Z]{2}[0-9]{6}|[0-9]{8})?$/"
                   ^                         ^^^^ 

请参阅regex demo

现在,模式将匹配:

  • ^ - 字符串开头
  • ( - 开始分组:
    • [a-zA-Z]{2}[0-9]{6} - 2个字母和6个数字
    • | - 或
    • [0-9]{8} - 8位数字
  • )? - 分组结束且?量词使其匹配1或0次(可选)
  • $ - 字符串结束。

答案 1 :(得分:2)

您可以尝试此DEMO LINK HERE

^(([a-zA-Z]{2}|[0-9]{2})[0-9]{6})?$

它会接受:

  1. ab123456
  2. 12345678
  3. aa441236
  4. aw222222