pattern = /domain\.com\/go\/(.*)3/i;
//alias always ends with "3"
url1 = "domain.com/go/firstname3";
url2 = "domain.com/go/secondname3?p=123";
console.log(url1.match(pattern)[1]); //firstname
console.log(url2.match(pattern)[1]); //secondname3?p=12
在两个例子中,我都需要返回参数,而不需要" 3" ("名字","第二名")。这意味着我必须找到char" 3"的第一个条目。第一个例子正常工作
我该如何更改模式?
感谢。
答案 0 :(得分:4)
使用GC
代替\w*
.*
或
pattern = /domain\.com\/go\/(\w*)3/i;
答案 1 :(得分:1)
这是因为您正在重复捕获组,并且所有语言实现仅保留最后一个捕获组。换句话说,如果在捕获secondname
和secondname3?p=12
之间有选择,Javascript将选择最后一个,即使两者同样有效。 This answer解释了问题的核心。
解决此类问题的正确方法是重写正则表达式组,使您想要检索的参数明确无误 - 通常通过更改.*
的出现次数来完成(注意它是.
个字符这是有问题的)更适合的事情,例如\w*
。您希望避免重复捕获组。我将解释为什么\w
这样做。
这里\w
表示单词元字符 - 它与a-z,A-Z和0-9匹配,没有别的。在这种特殊情况下,正则表达式无法再与secondname3?p=12
匹配,因为它包含=
,而\w
不允许package "name";
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by name on 26-Mar-16.
*/
public class Test extends Activity {
private static final String DB_NAME = "";
private static final String TABLE_NAME = "";
private SQLiteDatabase database;
private List<String> testContactName = new ArrayList<>();
public MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ViewPager mViewPager = (ViewPager) findViewById(R.id.testvp);
mAdapter = new MyAdapter();
mViewPager.setAdapter(mAdapter);
mViewPager.setCurrentItem(0);
mViewPager.setOffscreenPageLimit(50);
getAllContacts();
}
public void getAllContacts() {
DatabaseHelper dbOpenHelper = new DatabaseHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
String query = "select * from " + TABLE_NAME;
Cursor cursor = database.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
for (int c = 0; c < cursor.getCount(); c++) {
String name = cursor.getString(cursor.getColumnIndex("content"));
cursor.moveToNext();
/* String count= String.valueOf(cursor.getCount());
Toast.makeText(getApplicationContext(), count , Toast.LENGTH_LONG).show();*/
testContactName.add(name);
// testContactName.notifyAll();
Log.i("Name: " + "" + "---", name);
}mAdapter.notifyDataSetChanged();
}
cursor.close();
}
private class MyAdapter extends PagerAdapter {
public MyAdapter() {
super();
}
@Override
public int getCount() {
return testContactName.size();
}
@Override
public boolean isViewFromObject(View collection, Object object) {
return object == collection;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test1, null);
TextView pagenumber;
pagenumber = (TextView) view.findViewById(R.id.tv_test1);
try {
pagenumber.setText(testContactName.get(position));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
( collection).addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
( collection).removeView((View) view);
mAdapter.notifyDataSetChanged();
}
}
}
。因此,为什么@Avinash Raj的解决方案有效 - 即使重复捕获组,整个事物只有一个匹配的实例。