嗨,我只是从Regexs开始,并且正在尝试编写一个正则表达式来查找大括号内的字符串,同时保持大括号
@SuppressWarnings({"JavaReflectionMemberAccess", "deprecation"})
public static void setCursorDrawableColor(EditText editText, int color) {
try {
Field cursorDrawableResField = TextView.class.getDeclaredField("mCursorDrawableRes");
cursorDrawableResField.setAccessible(true);
int cursorDrawableRes = cursorDrawableResField.getInt(editText);
Field editorField = TextView.class.getDeclaredField("mEditor");
editorField.setAccessible(true);
Object editor = editorField.get(editText);
Class<?> clazz = editor.getClass();
Resources res = editText.getContext().getResources();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Field drawableForCursorField = clazz.getDeclaredField("mDrawableForCursor");
drawableForCursorField.setAccessible(true);
Drawable drawable = res.getDrawable(cursorDrawableRes);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawableForCursorField.set(editor, drawable);
} else {
Field cursorDrawableField = clazz.getDeclaredField("mCursorDrawable");
cursorDrawableField.setAccessible(true);
Drawable[] drawables = new Drawable[2];
drawables[0] = res.getDrawable(cursorDrawableRes);
drawables[1] = res.getDrawable(cursorDrawableRes);
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
cursorDrawableField.set(editor, drawables);
}
} catch (Throwable t) {
Log.w(TAG, t);
}
}
我已经在这里仔细查看了答案: Regular expression to find string inside curly brackets Javascript
这几乎可以满足我的要求,只是它只返回大括号内的内容。我想保留大括号。
答案 0 :(得分:1)
var myString = "This is a {nice} text to search for a {cool} substring",
pattern = /\{[^{}]*\}/g;
console.log(myString.match(pattern));
这是对您链接的问题的答案的修改。我在模式的开头添加了{
,并在结尾处删除了}
周围的前瞻。